File I/O
The FILE * Data type
- In C, the
FILE *
type is commonly used to represent a stream. - Streams are largely categorized 2 ways: text and binary.
- The file pointer is automatically updated by the standard library to point to the next character in the file after each read operation.
Reading text files
- Text files are logically a sequence of lines separated by newlines.
- Read char by char
- Read line by line
- Read and store values on variables
Writing Text files
Writing bytes to files:
- With binary files, we only get a raw stream of bytes, that’s all.
- Because it’s a stream of bytes, it can contain NUL characters.
- Use
fread
andfwrite
instead.
Reading bytes to files
Struct and Number Caveats
=> Solution: serialization (like Protocol Buffer)