Fscanf reads data from text files

Source: Internet
Author: User

Organized from: http://www.programfan.com/club/showtxt.asp? Id = 252333

Hello everyone! When I use the fscanf function to read data from a text file, I want to read only the first few characters in a row, but not all the subsequent characters, and then read the next row. For example, my data file is as follows:

23 456 # Number
78 981 # Number

I use

Fscanf ("% d \ n", & A, & B );
Fscanf ("% d \ n", & C, & D );

The objective is to make a = 23, B = 456, c = 78, D = 981. When reading, you want to ignore the "# Number" string after each row. But I failed. How can I control the write format? You do not want to define another string variable to read this string. Thank you !!

----------------------------------------------------------------------

Reference:

Fscanf ("% d % * s", & A, & B );
Fscanf ("% d % * s", & C, & D );

Brother, I have tried this method earlier. It does not work if there are multiple strings separated by spaces.

--------------------------------------------------------------------

Reference:

You didn't say there are multiple strings.

Fscanf ("% d % * [^ \ n] % * C", & A, & B );
Fscanf ("% d % * [^ \ n] % * C", & C, & D );

Thank you! After testing, the above method is good. At the same time, it is found that % * C in the format string can be changed to \ n. In addition, if you want to read numeric variables rather than character variables from the next row, it is no problem to omit % * C or \ n. However, if the next character is a character variable that needs to be read from the next row, % * C or \ n cannot be omitted.
----------------------------------------------------------------------

 

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.