W24-BinFile

W24-BinFile - Binary File_I/O 1 File Mode binary files mode...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
11/28/05 1 Binary File_I/O 11/28/05 2 File Mode - binary files mode can be "rb", "wb", "ab", "rb+","wb+". • "rb" open file for reading binary data from the beginning (fread). • "wb" open file for writing binary data from the beginning (fwrite). • "ab" open file for writing binary data starting at the end (fwrite). • "rb+" or "wb+" open for reading or writing. 11/28/05 3 Binary File Storage • In binary files numbers, characters and strings are stored the same as in memory. – 'a' stored as the ASCII code for a . – "a string" stored as ASCII codes for a string . – 1234567890 (a 4 byte int) stored as the 4 bytes. – 123.456 (a 4 byte float) stored as the 4 bytes. • No white space used. • This is a compact and efficient method of storage.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
11/28/05 4 Writing a Binary File FILE *fp; int a = 10; float b = 1.2345; char c[] = "ABC"; fp = fopen("output.dat", "wb"); fwrite(&a, sizeof(a), 1, fp); fwrite(&b, sizeof(b), 1, fp); fwrite(c, sizeof(c), 1, fp); fclose(fp); • This program used to generate data used on
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 4

W24-BinFile - Binary File_I/O 1 File Mode binary files mode...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online