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() will be used differently, reading in just one number.

To read from a binary file

1.
Create a new file or project in your text editor or IDE.

2.
Type the standard beginning lines of code (Script 12.6):

/* binary_read.c - Script 12.6 */
#include <stdio.h>

Script 12.6. You can read a binary file in a linear fashion using a program like this one.


3.
Set the file path and name as a C preprocessor macro:

#define THEFILE
 "/Users/larry/Desktop/numbers.dat"

or

#define THEFILE "C:\Documents and
 Settings\Larry Ullman\Desktop\
 numbers.dat"

Again, your directive should point to the same numbers.dat or just numbers file used by the binary_write application (Script 12.5).

4.
Create a constant macro representing the number of items in the array:

#define ITEMS 50

This value will again be used repeatedly in this application, so we define it as a preprocessor directive.

5.
Begin the main function and create a file pointer:

int main (void) {
FILE *fp;

6.
Define the required variables:

int i;
int numbers[ITEMS];

These variables correspond to those in the previous example.

7.
Open the file for binary reading:

fp = fopen(THEFILE, "rb");

The r mode is used to only read from a file; the b indicates it's a binary file.

8.
Start a conditional based on the file pointer and print a caption:

if (fp != NULL) {
printf("The contents of the numbers
 file:
");

9.
Read the entire contents of the file into the numbers array:

fread (numbers, sizeof(int), ITEMS,
 fp);

This call to fread() says that it should read ITEMS number of blocks of sizeof(int) length, and store this data in the numbers array. This is essentially the inverse of the fwrite() call in binary_write.c.

10.
Print each element of the array:

for (i = 0; i < ITEMS; i++) {
    printf("%d
", numbers[i]);
}

Since the numbers array has now been populated with the data from the numbers file, it can be printed in a for loop, like any array.

11.
Complete the fp conditional:

} else {
    printf ("The file could not
 be opened.
");
    return 1;
}

12.
Close the file:

if (fclose(fp) != 0) {
    printf ("The file could not
 be closed.
");
}

13.
Complete the main function:

    getchar();
    return 0;
}

14.
Save the file as binary_read.c, compile, and debug as necessary.

15.
Run the application (Figure 12.12).

Figure 12.12. The binary contents of the numbers file are retrieved and then printed in text format. (Only part of the results are shown here.)


✓ Tips

  • If you wanted, you could store the definitions of ITEMS and THEFILE in their own header file, which is then included by all three binary examples in this chapter. That way, a simple edit to the one file would keep each application working properly.

  • The portability of a binary file may be limited due to how different processors store multibyte data. A big endian machine orders the most important bytes of the data first. A little endian machine orders the most important bytes last. Consequently, a binary file written on one type of computer may not be properly read on the other type.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset