sscanf用法 - 如何验证已完成扫描与中止扫描

Joh*_*nes 3 c formatting scanf

我的数据库提供了一个文本文件,其中包含"分隔符公式的开始和结束.公式集非常有限,一旦确定就很容易实现.我尝试使用scanf来获取参数,我想使用分隔符"来提供scanf失败的机制.

在下面的示例中,将忽略最后一个分隔符,并且丢失未找到分隔符的信息.如果sscanf能够匹配整个字符串,我该如何控制?

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

unsigned printIdentity(const char * formula){
    unsigned e = 0, found = 0;
    double a, b;
    printf("-------------\n");
    printf("INVESTIGATING: %s\n", formula);
    if( ( 2 == sscanf_s( formula, " \" X * %lf %lf \" ", &a, &b, sizeof( double ), sizeof( double ) ) ) ){
        printf("MATCH: X * %lf + %lf\n", a, b);
        ++found;
    }
    if( ( 1 == sscanf_s( formula, " \" X * %lf \" ", &a, sizeof( double ) ) ) ){
        printf("MATCH: X * %lf\n", a);
        ++found;
    }
    if( found != 1){
        e += 1;
        printf("ERROR: %u formula types\n", found);
    }
    printf("-------------\n");
    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\n", e ); }
    else{ printf( "all pass\n", e ); }
    return e;
}
Run Code Online (Sandbox Code Playgroud)

hmj*_*mjd 6

如果sscanf能够匹配整个字符串,我该如何控制?

使用格式说明符%n获取处理结束的位置,并与输入字符串的长度进行比较,其中n格式说明符定义为(来自第7.19.6.2节C99标准的fscanf函数):

没有输入消耗.相应的参数应该是一个指向有符号整数的指针,到目前为止,通过调用fscanf函数将写入从输入流中读取的字符数.执行%n指令不会增加fscanf函数执行完成时返回的赋值计数.没有参数被转换,但是消耗了一个参数.如果转换规范包含赋值压缩字符或字段宽度,则行为未定义.

例如:

#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\n", 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\n", a[0], a[1]);
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

good_input: 20 21

在线演示http://codepad.org/6G4lLXWg.