> ~ Online tutorial


fread() and fwrite()
These functions read and write whole blocks of characters at a time. The form of fread() is as follows:
FILE *fp;
int  noread,n,size;
char *ptr;

noread = fread (ptr,size,n,fp);

The parameters in parentheses provide information about where the data will be stored once they have been read from a file. fp is a pointer to an open file; ptr is a pointer to the start of a block of memory which is to store the data when it is read; size is the size of a block of data in characters; n is the number of blocks of data to be read. Finally noread is a return value which indicates the number of blocks which was actually read during the operation. It is important to check that the number of blocks expected is the same as the number received because something could have gone wrong with the reading process. (The disk might be corrupted or the file might have been altered in some way.) fwrite() has an identical call structure to fread():
FILE *fp;
int  nowritten,n,size;
char *ptr;

nowritten = fread (ptr,size,n,fp);

This time the parameters in parentheses provide information about where the data, to be written to a file, will be found. fp is a pointer to an open file; ptr is a pointer to the start of a block of memory at which the data are stored; size is the size of a "block" of data in characters; n is the number of blocks of data to be read; nowritten is a return value which indicates the actual number of blocks which was written. Again, this should be checked.
A caution about these functions: each of these block transfer routines makes an important assumption about the way in which data are stored in the computer system. It is assumed that the data are stored contiguously in the memory, that is, side by side, in sequential memory locations. In some systems this can be difficult to arrange (in multi-tasking systems in particular) and almost impossible to guarantee. Memory which is allocated in C programs by the function malloc() does not guarantee to find contiguous portions of memory on successive calls. This should be noted carefully when developing programs which use these calls.

Please Give Us Your 1 Minute In Sharing This Post!
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Powered By: BloggerYard.Com

0 comments: