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

Reading from Binary Files

As you might expect, reading from binary files is just the opposite of writing to them. To do so, you'll make use of the fread() function:

fread (var_pointer, size, blocks, fp);

The first argument is a pointer to, or the address of, the variable to which the read data should be assigned. The second value is the size (in terms of bytes) of one block of data to be read in. This value should correspond to the size of the receiving variable. The blocks argument works like its counterpart in fwrite(): dictating how many chunks of data to read in. Finally, the file pointer is referenced.

In our next example, we use fread()to read the entire contents of the data file back into an array. In the subsequent example, fread() ...

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