Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
616 views
in Technique[技术] by (71.8m points)

c - sscanf usage - how to verify a completed scan vs an aborted scan

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How can i control if sscanf was able to match the whole string?

Use the format specifier %n to obtain the position where processing ended and compare with the length of the input string, where the n format specifier is defined as (from section 7.19.6.2 The fscanf function of the C99 standard):

No input is consumed. The corresponding argument shall be a pointer to signed integer into which is to be written the number of characters read from the input stream so far by this call to the fscanf function. Execution of a %n directive does not increment the assignment count returned at the completion of execution of the fscanf function. No argument is converted, but one is consumed. If the conversion specification includes an assignmentsuppressing character or a field width, the behavior is undefined.

For example:

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

int main()
{
    const char* good_input = ""20 21"";
    const char* bad_input = ""14 12";
    int a[2];
    int pos;

    if (sscanf(good_input, " "%d %d"%n", &a[0], &a[1], &pos) == 2 &&
        pos == strlen(good_input))
    {
        printf("good_input: %d %d
", a[0], a[1]);
    }

    if (sscanf(bad_input, " "%d %d"%n", &a[0], &a[1], &pos) == 2 &&
        pos == strlen(bad_input))
    {
        printf("bad_input: %d %d
", a[0], a[1]);
    }
}

Output:

good_input: 20 21

Online demo at http://codepad.org/6G4lLXWg .


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...