This section collects the exercises for the chapter.
They are small on purpose. Each one should test one rule about files, streams, buffers, or errors.
Exercise 13-1. Open a file named message.txt and print its contents.
Exercise 13-2. Change the program so the filename comes from the command line.
Exercise 13-3. If the file cannot be opened, print an error message to standard error.
Exercise 13-4. Count the number of bytes in a file.
Exercise 13-5. Count the number of newline bytes in a file.
Exercise 13-6. Copy one file to another.
Exercise 13-7. Change the copy program so it refuses to overwrite an existing file.
Exercise 13-8. Change the copy program so it deletes the output file if writing fails.
Exercise 13-9. Write a program that copies standard input to standard output.
Exercise 13-10. Write a program that copies standard input to a file.
Exercise 13-11. Write a program that copies a file to standard output.
Exercise 13-12. Write a program that prints all command-line arguments, one per line.
Exercise 13-13. Write a program that writes the numbers 1 through 100 to a file.
Exercise 13-14. Write a program that reads a file in 1-byte chunks.
Exercise 13-15. Change the previous program to read in 4096-byte chunks. Compare the structure.
Exercise 13-16. Write a program that copies a file using a fixed buffer.
Exercise 13-17. Write the same copy program using buffered input and buffered output.
Exercise 13-18. Write a program that reads standard input and counts bytes.
Exercise 13-19. Extend it so it also counts lines.
Exercise 13-20. Extend it so it also counts words, where a word is a maximal run of non-whitespace bytes.
Exercise 13-21. Write a program that prints the first 10 bytes of a file.
Exercise 13-22. Print those bytes in hexadecimal.
Exercise 13-23. Write a function:
fn firstByte(path: []const u8) !?u8It should return the first byte of the file, or null if the file is empty.
Exercise 13-24. Write a function:
fn copyFile(src_path: []const u8, dst_path: []const u8) !voidUse defer for closing files.
Exercise 13-25. Modify copyFile so it uses errdefer to delete the destination file if the copy fails.
Exercise 13-26. Write a function:
fn writeText(path: []const u8, text: []const u8) !voidIt should create the file and write the full text.
Exercise 13-27. Rewrite writeText using a buffered writer.
Exercise 13-28. In the buffered version, remove flush. Observe what happens.
Exercise 13-29. Write a filter that converts ASCII lowercase letters to uppercase.
Exercise 13-30. Write a filter that removes empty lines.
Exercise 13-31. Write a filter that numbers lines.
Exercise 13-32. Write a filter that replaces tab bytes with four spaces.
Exercise 13-33. Write a program that writes diagnostics to standard error and ordinary output to standard output.
Exercise 13-34. Rewrite an older Zig output example using the Zig 0.16 std.Io style.
Exercise 13-35. Write a complete cat program.
Exercise 13-36. Write a complete cp program.
These exercises should leave you with the basic pattern of Zig I/O: open a resource, use an explicit buffer, read or write slices, handle errors, and close what you open.