? Because of the different storage methods, reading should also be operated in a corresponding way, otherwise the stored data can not be obtained correctly.
Binary access
The binary way is to store the memory format of the original data in a file, which can be read correctly on the same type of machine. However, if you switch to a different type of machine, there will be data interpretation errors, so it is not portable.
In this way, the access functions used are fwrite () and fread ().
For example:
int num = 12345;
FILE *fp=fopen( "datafile "," WB ");
fwrite(& amp; num,sizeof(int), 1,FP); //Write num data to a file
....
FILE *fp=fopen( "datafile "," Rb ");
Fred (&num, sizeof(int), 1, fp); //read file data into num
Character access
Character mode is to store the visible content of data in a file in the form of characters, which can be read on any machine to facilitate data transmission between different systems.
In this way, the access functions used are fprintf () and fscanf ().
For example:
int num = 0;
FILE *fp=fopen( "datafile "," w ");
fprintf(fp," %d\n ",num); //Write num data to a file
....
FILE *fp=fopen( "datafile "," r ");
fscanf(fp," %d ",& ampnum); //read file data into num