scanf (), FSCANF's explanation

Source: Internet
Author: User

We only discuss the format of fscanf (or scanf) here, because these details are not covered in other stickers, and you can take a few detours when reading this article. Only the results, the underlying causes are not analyzed.

FILE *pfile;float X1; Char name[20];

PFile = fopen (' "Example.txt", "R");

1. Parameter format:

such as fscanf (PFile, "%s%f", name, &X1) can correctly read the value, then when the%f in this sentence is replaced with%LF will be an error. Conversely, changing the X1 variable declaration float to double will also make an error. This means that fscanf cannot complete the conversion between float and double, and if the FSCANF call format does not match, it will produce unexpected results.

2. Number of parameters:

declare float x2; then call fscanf (PFile, "%s%f", Name, &X1,&X2) This statement can be executed completely correctly, except that X2 is not assigned a value. However, when the FSCANF statement is changed to FSCANF (PFile, "%s%f%f", Name, &x1), an error occurs.

Valid input for 3.Fomat: "%s\r\f%f h" like this input, the compiler will automatically parse only the meaningful part, namely: "%s%f".

You are welcome to correct me, or help me explain the deep mechanism and learn from each other.

scanf (), FSCANF's explanation

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.