fread and fwrite. Both functions has a parameter
to specify how big (in bytes) each ``thing'' is, and how many things
to read or write.
In Linux and all POSIX compliant operating systems, fopen can
no longer open a file as ``text'' or ``binary''. In a POSIX compliant
operating system, a file is just a file. The distinction between
``cooked'' versus ``raw'' is at the TTY (teletype) device level, not
a per file level. You can use the function termios (and other
related functions) to control attributes of a terminal device.
The following code illustrates how to perform structure-based binary I/O operations:
{
FILE *pFile;
struct X someStruct;
// open a file so pFile points to the stream
// stuff someStruct with data
if (!fwrite(&someStruct, sizeof(someStruct), 1, pFile))
{
// error handling
// why can't we write to the file?
}
// to read from pFile
if (!fread(&someStruct, sizeof(someStruct), 1, pFile))
{
// error handling
// why can't we read from the file? EOF?
}
}