scanf格式字符串

时间:2012-12-21 09:31:45

标签: c regex scanf

我正在尝试学习scanf格式的字符串,而且我无法让它工作。我试图读取格式的字符串:

" someKey" =" someValue中"

这是我正在使用的代码:

void test()
{
    char buffer[2][128];
    int amountRead;
    char* input = "\"test\"=\"hello\"";
    int result = sscanf(input, "\"%128[a-zA-Z0-9]s\"=\"%128[a-zA-Z0-9]s\"%n", buffer[0], buffer[1], &amountRead);
    printf("input = %s\nresult = %d\nstr1 = %s\nstr2 = %s\namountread = %d\n", input, result, buffer[0], buffer[1], amountRead);
}

出于某种原因,它只设法扫描第一个值:

input = "test"="hello"
result = 1
str1 = test
str2 = 
amountread = 0

1 个答案:

答案 0 :(得分:8)

稍微使用了您的代码,然后转到联机帮助页。

  

s匹配一系列非空白字符;

...

  

[匹配指定的一组接受字符中的非空字符序列;

显然,在使用[时,您不需要s,因此以下内容可以正常运行:

int result = sscanf(input, "\"%128[a-zA-Z0-9]\"=\"%128[a-zA-Z0-9]\"%n", buffer[0], buffer[1], &amountRead);