sscanf: %* 不忽略空字段
sscanf: %* doesn't ignore empty fields
从文件中读取一行,使用 sscanf 和 %*
来忽略像这样的不需要的字段
int count = sscanf(linha,
"%*[^\t]\t%4[^\t]"
&livro->area);
仅当字段有字符时有效
"20/out/17\tINF\t"
但是当要忽略的字段为空时它不起作用 "\tINF\t"
有人可以向我解释为什么会这样吗?我怎样才能让它在空的时候也忽略它?
提前致谢
来自 %[...]
说明符的 description(强调已添加):
Matches a nonempty sequence of characters from a set of expected characters (the scanset).
所以它永远不会匹配空字符串。
也许您应该使用 strtok()
而不是 sscanf()
。
how I can make it also ignore it if empty?
由于 sscanf(s, "%*[^\t]", ....)
不会忽略空字段,因此使用 2 sscanf()
并记录第一次扫描的距离 - 如果有的话。
int offset = 0; // Use this value if the below sscanf() fails to reach %n
sscanf(linha, "%*[^\t]%n", &offset);
int count = sscanf(linha + offset, "\t%4[^\t]", &livro->area);
从文件中读取一行,使用 sscanf 和 %*
来忽略像这样的不需要的字段
int count = sscanf(linha,
"%*[^\t]\t%4[^\t]"
&livro->area);
仅当字段有字符时有效
"20/out/17\tINF\t"
但是当要忽略的字段为空时它不起作用 "\tINF\t"
有人可以向我解释为什么会这样吗?我怎样才能让它在空的时候也忽略它? 提前致谢
来自 %[...]
说明符的 description(强调已添加):
Matches a nonempty sequence of characters from a set of expected characters (the scanset).
所以它永远不会匹配空字符串。
也许您应该使用 strtok()
而不是 sscanf()
。
how I can make it also ignore it if empty?
由于 sscanf(s, "%*[^\t]", ....)
不会忽略空字段,因此使用 2 sscanf()
并记录第一次扫描的距离 - 如果有的话。
int offset = 0; // Use this value if the below sscanf() fails to reach %n
sscanf(linha, "%*[^\t]%n", &offset);
int count = sscanf(linha + offset, "\t%4[^\t]", &livro->area);