C + + fscanf reads a whole line

Source: Internet
Author: User

scanf, FSCANF is similar, reads input from the stream and assigns a value to the variable

int scanf (const char *format, ...)

int fscanf (FILE *stream, const char *format,...)

%c single character

%[] a character set

* Input entries are skipped after they are read in, and no variables are assigned

scanf, fscanf automatically skips tabs and spaces, other characters give variables, returns the number of variables (integers) that were successfully assigned, and returns EOF when an error occurs

%[^\n] means reading a whole line, ^ means "non", the previous reference to%[] represents a character set, so%[^\n] represents "a collection of all characters before a newline", and then ends the read-in.

But after doing this, the newline character is not yet read.

As mentioned earlier, * indicates that the input is skipped after it is read in, so%*c reads a character, but does not assign any variable, so it can be written

FSCANF (FP,%[^\n]%*c, str)

Where FP is a file stream pointer, str is a string variable, this code means: read everything before the line break, assign to STR, and then skip the line break, pointing to the next line of content.

C + + fscanf reads a whole line

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.