O'Reilly logo

C Programming: Visual Quickstart Guide by Marc Liyanage, Larry Ullman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Writing to Binary Files

At the very beginning of this chapter, the two file types were mentioned: plain text (formally called ASCII) and binary. All of the previous examples have used plain-text files, which are human readable, but you can also write to and read from binary files using C. The basic ideas for working with binary files are the same, but the specific functions are different.

Binary files are opened and closed like ASCII files, except that you should use the b modifier on the mode:

FILE *fp;
fp = fopen ("thefile", "wb");
// Write to the file.
fclose(fp);

Once you've successfully opened the file, you can write data to it using fwrite(). Its syntax is more complex than you've seen so far:

fwrite (write_data_pointer,
					
					bytes_to_write, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required