where buffer is a pointer to the region in memory from which the data is to be read or written respectively, num_bytes is the number of bytes in each item to be read or written, and count is the total number of items ( each num_bytes long ) to be read/written. The functions return the number of items successfully read or written.
NOTE : Unlike all the other functions we have encountered so far fread and fwrite read and write binary data in the same format as it is stored in memory so if we try to edit one these files it will appear completely garbled. Functions like fprintf, fgets, etc. read and write displayable data. fprintf will write a double as a series of digits while fwrite will transfer the contents of the 8 bytes of memory where the double is stored directly.
Random Access I/O:
The fseek() function is used in C to perform random access I/O and has the following prototype.
int fseek ( FILE *fp, long num_bytes, int origin ) ;
where origin specifies one of the following positions as the origin in the operation
SEEK_SET --- beginning of file
SEEK_CUR --- current position
SEEK_END --- EOF
and where num_bytes is the offset in bytes to the required position in the file. fseek() returns zero when successful, otherwise a non-zero value.
For Example if we had opened a file which stored an array of integers and we wish to read the 50th value we might do the following