My database provides a textfile with opening and closing "
to delimiter formulas.
The set of formulas is very limited and will be easy to implement once identified.
I try to use scanf to get the parameters and i want to use the delimiter "
to provide a mechanism for scanf to fail.
In the below example the last delimiter is ignored and the information that the delimiter was not found is lost. How can i control if sscanf was able to match the whole string?
#include <stdio.h>
#include <string.h>
unsigned printIdentity(const char * formula){
unsigned e = 0, found = 0;
double a, b;
printf("-------------
");
printf("INVESTIGATING: %s
", formula);
if( ( 2 == sscanf_s( formula, " " X * %lf %lf " ", &a, &b, sizeof( double ), sizeof( double ) ) ) ){
printf("MATCH: X * %lf + %lf
", a, b);
++found;
}
if( ( 1 == sscanf_s( formula, " " X * %lf " ", &a, sizeof( double ) ) ) ){
printf("MATCH: X * %lf
", a);
++found;
}
if( found != 1){
e += 1;
printf("ERROR: %u formula types
", found);
}
printf("-------------
");
return e;
}
unsigned main( void )
{
unsigned e = 0;
e += printIdentity(" "X*3.1"");
e += printIdentity(" "X*3.2-4.2"");
e += printIdentity(" "X*3.3+4.3"");
if( 0 != e ){ printf( "ERRORS: %2u
", e ); }
else{ printf( "all pass
", e ); }
return e;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…