A byte stream is a file stream whose bytes pass between the external file and the program without text-mode translation. Opening a stream in binary mode preserves each byte as read or written, avoiding changes caused by end-of-line conventions. section 14.4
TAKEAWAY: Use the binary-stream interfaces to move through file data deliberately.
- Open the stream with a binary mode such as "rb", "wb", or "ab"; use the update forms when reading and writing the same stream. section 14.4 section 8.3.2
- Use fread and fwrite to transfer size * nmemb bytes from the current file position, and treat a smaller return value as an error. section 14.4
- Use ftell to inspect the byte position and fseek with SEEK_SET or SEEK_CUR to move within the stream. section 14.4
Copying an object representation
A program can write a large C object’s internal representation with fwrite, later reposition the stream with fseek, and read the bytes back with fread. Binary mode keeps the file contents unmodified; the return counts provide a check that the expected transfer completed. section 14.4
1 size_t fread(void* restrict ptr, size_t size, size_t nmemb,
2 FILE* restrict stream);
3 size_t fwrite(void const*restrict ptr, size_t size, size_t nmemb,
4 FILE* restrict stream);
5 int fseek(FILE* stream, long int offset, int whence);
6 long int ftell(FILE* stream);
!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":1}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":10}],[{\"line\":2,\"ch\":4},{\"line\":2,\"ch\":10}],[{\"line\":0,\"ch\":11},{\"line\":0,\"ch\":16}],[{\"line\":0,\"ch\":17},{\"line\":0,\"ch\":21}],[{\"line\":2,\"ch\":18},{\"line\":2,\"ch\":22}],[{\"line\":0,\"ch\":23},{\"line\":0,\"ch\":31}],[{\"line\":1,\"ch\":23},{\"line\":1,\"ch\":31}],[{\"line\":2,\"ch\":29},{\"line\":2,\"ch\":37}],[{\"line\":3,\"ch\":24},{\"line\":3,\"ch\":32}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":10}],[{\"line\":2,\"ch\":4},{\"line\":2,\"ch\":10}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":10}],[{\"line\":2,\"ch\":4},{\"line\":2,\"ch\":10}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":1}],[{\"line\":1,\"ch\":17},{\"line\":1,\"ch\":21}],[{\"line\":3,\"ch\":18},{\"line\":3,\"ch\":22}],[{\"line\":4,\"ch\":14},{\"line\":4,\"ch\":18}],[{\"line\":5,\"ch\":19},{\"line\":5,\"ch\":23}],[{\"line\":0,\"ch\":23},{\"line\":0,\"ch\":31}],[{\"line\":1,\"ch\":23},{\"line\":1,\"ch\":31}],[{\"line\":2,\"ch\":29},{\"line\":2,\"ch\":37}],[{\"line\":3,\"ch\":24},{\"line\":3,\"ch\":32}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":1}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":10}],[{\"line\":2,\"ch\":4},{\"line\":2,\"ch\":10}],[{\"line\":2,\"ch\":11},{\"line\":2,\"ch\":17}],[{\"line\":2,\"ch\":18},{\"line\":2,\"ch\":28}],[{\"line\":0,\"ch\":23},{\"line\":0,\"ch\":31}],[{\"line\":1,\"ch\":23},{\"line\":1,\"ch\":31}],[{\"line\":2,\"ch\":29},{\"line\":2,\"ch\":37}],[{\"line\":3,\"ch\":24},{\"line\":3,\"ch\":32}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":10}],[{\"line\":2,\"ch\":4},{\"line\":2,\"ch\":10}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":10}],[{\"line\":2,\"ch\":4},{\"line\":2,\"ch\":10}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":1}],[{\"line\":1,\"ch\":17},{\"line\":1,\"ch\":21}],[{\"line\":3,\"ch\":18},{\"line\":3,\"ch\":22}],[{\"line\":4,\"ch\":14},{\"line\":4,\"ch\":18}],[{\"line\":5,\"ch\":19},{\"line\":5,\"ch\":23}],[{\"line\":0,\"ch\":23},{\"line\":0,\"ch\":31}],[{\"line\":1,\"ch\":23},{\"line\":1,\"ch\":31}],[{\"line\":2,\"ch\":29},{\"line\":2,\"ch\":37}],[{\"line\":3,\"ch\":24},{\"line\":3,\"ch\":32}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":1}],[{\"line\":4,\"ch\":4},{\"line\":4,\"ch\":7}],[{\"line\":5,\"ch\":9},{\"line\":5,\"ch\":12}],[{\"line\":4,\"ch\":8},{\"line\":4,\"ch\":13}],[{\"line\":1,\"ch\":17},{\"line\":1,\"ch\":21}],[{\"line\":3,\"ch\":18},{\"line\":3,\"ch\":22}],[{\"line\":4,\"ch\":14},{\"line\":4,\"ch\":18}],[{\"line\":5,\"ch\":19},{\"line\":5,\"ch\":23}],[{\"line\":4,\"ch\":28},{\"line\":4,\"ch\":36}],[{\"line\":5,\"ch\":4},{\"line\":5,\"ch\":12}],[{\"line\":4,\"ch\":4},{\"line\":4,\"ch\":7}],[{\"line\":5,\"ch\":9},{\"line\":5,\"ch\":12}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":1}],[{\"line\":4,\"ch\":28},{\"line\":4,\"ch\":36}],[{\"line\":5,\"ch\":4},{\"line\":5,\"ch\":12}],[{\"line\":5,\"ch\":13},{\"line\":5,\"ch\":18}],[{\"line\":1,\"ch\":17},{\"line\":1,\"ch\":21}],[{\"line\":3,\"ch\":18},{\"line\":3,\"ch\":22}],[{\"line\":4,\"ch\":14},{\"line\":4,\"ch\":18}],[{\"line\":5,\"ch\":19},{\"line\":5,\"ch\":23}]]"}
!@%STYLE%@!
Browse the compact C interface for block transfer and file-position control. section 14.4


