C Standard I/O_fread/fwrite
I haven't used it for a long time. I am a little unfamiliar with C language file operations. I need to review it a little;
The following program uses fread/fwrite in the C language to read and copy hex files, so that the copied files must not be any different from the source files;
// Hex. cpp: Defines the entry point for the console application. // # include "stdafx. h" # include
# Define SIZE 512int main (int argc, char * argv []) {int I = 0; int cnt = 0; int nRet = 0; char buf [SIZE]; FILE * fpr = NULL; FILE * fpw = NULL; if (fpr = fopen ("D: \ LED. hex "," r ") = NULL) fprintf (stderr," Open LED. hex error "); if (fpw = fopen (" D: \ test. hex "," w ") = NULL) fprintf (stderr," Open test. hex error "); while (nRet = fread (buf, 1, sizeof (buf), fpr ))! = 0) {for (I = 0; I
Fread (addr, size, num, fp); // size indicates the number of bytes read each time, and num indicates that data such as size needs to be read;
The number of bytes to be read is returned successfully. If the number of bytes to be read fails,-1 is returned. If the number of bytes to be read reaches the end of the file, 0 is returned;
The experiment found that as long as you read one byte each time, no matter what type of buffer your buf is, it can be read successfully and there will basically be no problems, I should have known about the C language. The following program is used to compare it and nRet is used to receive the number of bytes read by fread:
// Hex. cpp: Defines the entry point for the console application. // # include "stdafx. h" # include
# Include
# Define SIZE 256int main (int argc, char * argv []) {int I = 0; int cnt = 0; int nRet = 0; int flag = 1; short buf [SIZE]; FILE * fpr = NULL; FILE * fpw = NULL; if (fpr = fopen ("D: \ LED. hex "," r ") = NULL) fprintf (stderr," Open LED. hex error "); if (fpw = fopen (" D: \ test. hex "," w ") = NULL) fprintf (stderr," Open test. hex error "); while (nRet = fread (buf, 1, sizeof (buf), fpr ))! = 0) {if (flag = 1) {for (I = 0; I
Note that the two printing formats are different, because the first is char type and the second is short type, but here the verification file is intact, so we will not consider this;