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() ...

Get C Programming: Visual Quickstart Guide now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.