O'Reilly logo

Just Enough C/C++ Programming by Guy W. Lecky-Thompson

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

Using fread and fwrite

The two binary file-processing functions are fread and fwrite. They are similar in definition:

int fread ( void * data, int size, int count,
  FILE * file );

int fwrite ( void * data, int size, int count,
  FILE * file );

The first parameter is the data to be written or read, the second is the size of each data item, the third is the number of data items, and the final parameter is the file stream, opened with a call to fopen. The use of a void pointer in the first parameter is because it is not known what kind of data will be passed to the function until it is invoked.

In addition, because the first parameter is itself a pointer, it can be cast to any type, including a pointer (reference) to a single item of data.

The following ...

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