C Standard I/O_fread/fwrite

Source: Internet
Author: User

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;
       



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.