Unformatted data file

Unformatted data file

Some applications involve the use of data files to store block of data, where each block consists of a fixed number of contiguous bytes.

Each block will generally represent a complex data structure, such as a structure or an array.

For example, a data file may consist of multiple structures having the same composition, or it may contain multiple arrays of the same type and size.

For such applicators it may be desirable to read the entire block from the data, or write the entire block to the data file, rather than reading or writing the individual components (i.e., structure members of array elements) within each block separately.

The library functions fread and fwrite are intended to be used in situations of this type.

There functions are often referred to as unformatted read and write functions. Similarly, data files of this type are often referred to as unformatted data file.

Each of these functions requires four arguments: a pointer to the data block, the size of the data block, the number of data blocks being transferred, and the stream pointer.

Thus, a typical fwrite function might be written as: fwrite(&customer, sizeof(record), 1, fpt);

Where customer is a structure variable of type record, and fpt is the stream pointer associated with a data file that has been opened for output.

An example program to create an unformatted data file containing customer records:

Reading file:

Displaying the contents: