How to Use comma (,) as the separator sscanf example and fscanf example

Source: Internet
Author: User
I. sscanf example

/* The following sample has strates the use of brackets and
Caret (^) with sscanf ().
Compile options needed: None
*/

# Include <math. h>
# Include <stdio. h>
# Include <stdlib. h>

Char * tokenstring = "first, 25.5, second, 15 ";
Int result, I, RV;
Double FP;
Char o [10], F [10], s [10], t [10];

Void main ()
{
Result = sscanf (tokenstring, "% [^ ','], % [^ ','], % [^ ','], % s", O, S, t, F );
Fp = atof (s );
I = atoi (f );
Printf ("% s/n % lf/n % s/n % d/N", O, FP, T, I );
}

Ii. fscanf example

/* The following sample has strates the use of brackets and
Caret (^) with sscanf ().
Compile options needed: None
*/

# Include <math. h>
# Include <stdio. h>
# Include <stdlib. h>

Char * tokenstring = "first, 25.5, second, 15 ";
Int result, I;
Double FP;
Char o [10], F [10], s [10], t [10];

File * filep; // The open or close operations on the file are omitted here

Void main ()
{
Rv = fscanf (filep, "% s", tokenstring );
Result = sscanf (tokenstring, "% [^ ','], % [^ ','], % [^ ','], % s", O, S, t, F );

Fp = atof (s );
I = atoi (f );
Printf ("% s/n % lf/n % s/n % d/N", O, FP, T, I );
}
If you directly use fscanf to read the strings "first, 25.5, second, 15 ",
That is, fscanf (FP, "% [^ ','], % [^ ','], % [^ ','], % s", O, S, T, f );
The result will fail. I have not investigated the cause.

You must first read the strings stored in the file into tokenstring and convert the file operation into memory operations before the parsing is successful.

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.