sscanf的用法 - 如何验证完成扫描VS中止扫描 [英] sscanf usage - how to verify a completed scan vs an aborted scan
问题描述
我的数据库提供了一个文本文件与开闭来分隔公式。
这组公式是非常有限的,将很容易实现一次鉴定。
我尝试用scanf函数来获取参数,我想使用的分隔符来提供一个机制,scanf的失败。
在下面的例子中,最后一个分隔符被忽略,未找到的分隔符丢失的信息。如何控制,如果sscanf的是能够将整个字符串匹配?
的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;无符号printIdentity(为const char *公式){
无符号E = 0,找到= 0;
双A,B;
的printf(------------- \\ n);
的printf(调查中的应用:%S \\ n,公式);
如果((2 == sscanf_s(公式中,\\X *%LF%LF \\,&安培;一,和b,sizeof的(双),的sizeof(双)))){
的printf(MATCH:X *%LF + LF%\\ n,A,B);
++发现;
}
如果((1 == sscanf_s(式中,\\X *%LF \\,&放大器;一,sizeof的(双)))){
的printf(MATCH:X *%LF \\ n,一);
++发现;
}
如果(找到了!= 1){
E + = 1;
的printf(ERROR:%U公式类型的\\ n,找到);
}
的printf(------------- \\ n);
返回e的;
}无符号主要(无效)
{
无符号E = 0; E + = printIdentity(\\X * 3.1 \\);
E + = printIdentity(\\X * 3.2-4.2 \\);
E + = printIdentity(\\X * 3.3 + 4.3 \\); 如果(0 = E!){printf的(错误:%2U \\ n,E); }
其他{printf的(全通\\ n,E); }
返回e的;
}
我如何控制的sscanf能够整个字符串匹配?
块引用>使用格式说明
%N
来获得,其中处理结束的位置,并与输入字符串的长度进行比较,其中N
格式说明被定义为(从部分的 7.19.6.2 fscanf函数的C99标准):
没有输入被消耗。相应的参数是一个指向
符号整数是成要写入读取的字符数
到目前为止由此调用fscanf函数的输入流。的执行
%N指令不增加在返回的任务数
fscanf函数的执行的完成。不转换参数,
但一被消耗。如果转换规格包括assignmentsup pressing
字符或字段宽度,行为是不确定的。
块引用>例如:
的#include<&string.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&stdio.h中GT;诠释的main()
{
为const char * good_input =\\20 21 \\;
为const char * bad_input =\\14 12;
诠释一个[2];
INT POS; 如果(sscanf的(good_input,\\%D \\%N,&放大器;一个[0],&放大器;一个[1],&放大器;正)== 2和;&放大器;
POS == strlen的(good_input))
{
的printf(good_input:%D \\ n,一个[0],A [1]);
} 如果(sscanf的(bad_input,\\%D \\%N,&放大器;一个[0],&放大器;一个[1],&放大器;正)== 2和;&放大器;
POS == strlen的(bad_input))
{
的printf(bad_input:%D \\ n,一个[0],A [1]);
}
}输出:
good_input:20 21。在 HTTP
在线演示://$c$cpad.org/6G4lLXWg
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("-------------\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; }
解决方案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 then
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\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]); } }
Output:
good_input: 20 21Online demo at http://codepad.org/6G4lLXWg .
这篇关于sscanf的用法 - 如何验证完成扫描VS中止扫描的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!