The difference between fgets and fscanf

Source: Internet
Author: User
one, the function of the approximate difference:
①fgets: Read a row of data from the file into the buffer (Fgets encountered a carriage return will end, no space and carriage return to do any conversion to the buffer, after the end of the buffer to write more than one, so it is read a row of data)

②FSCANF: Read a piece of data from a file into the buffer (fscanf encounters a space or carriage return to the end, it will be a space or carriage return to the, so it is read a small piece of data)
second, for example: Copy the contents of the A.txt file to B.txt.

A.txt in the first line of "I Love you little white" and "joking" in the middle of a space, the end of each line has a carriage return:

I love you little white joke
haha
joking

1. Use fgets to read A.txt file and write to B.txt program fragment:

File *f1 = fopen ("A.txt", "R");//read-only mode opens A.txt, the file must exist with the filename
*f2 = fopen ("B.txt", "w");//Open B.txt in write-only mode, which can not exist
if (f1 = = NULL)//If the file does not exist, direct end
{return
    0;
}
Char buf[1024] = {0};//buffer while
(!feof (F1))
{
    memset (buf, 0, sizeof (BUF));/Empty buffer
    fgets (buf, sizeof (BUF), F1); Read a row of data from a file into a buffer (fgets encounters a carriage return to the end, no spaces and carriage returns do any conversion to the buffer, and then to the buffer to write more than one, so it is read a row of data)
    fputs (buf, F2);
}

Fclose (F1); Pay attention to closing the file
fclose (F2);//Note the last file close

Run Result:

2. Use fscanf to read A.txt file and write to B.txt program fragment:

File *f1 = fopen ("A.txt", "R");//read-only mode open a.txt
file *f2 = fopen ("B.txt", "w");//Open B.txt as write-only, it can not exist
if (f1 = = NUL L)//If the file does not exist, direct end
{return
    0;
}
Char buf[1024] = {0};//buffer while
(!feof (F1))
{
    memset (buf, 0, sizeof (BUF));/Empty buffer
    fscanf (F1, "%s", BUF);//reads a piece of data from a file into a buffer (fscanf encounters a space or carriage return ends, it converts a space or carriage return to a, so it reads a small piece of data)
    fputs (buf, F2);
}

Fclose (F1); Pay attention to closing the file
fclose (F2);//Note the last file close

Run Result:

Third, the conclusion

1, Fgets encountered a "space" and encountered the same as the usual characters read, encountered "carriage return" is the end of this read, and finally to the buffer (char *buf[]) The last plus more than one "" "means the end of this reading line.

2, fscanf whether encounter "space" or "carriage return", as ' read ' to the buffer (char  *buf[]), and end this read.

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.