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.