Getting error with sscanf_s in Visual Studio 2012
我正在尝试解析以下字符串-
1 2 3 4 5 | "DATA,100,10,1,9,82,60" "DATA,57,27,59,30,11,64" "DATA,12,86,100,97,103,23" "DATA,1,10,78,38,45,52" "DATA,99,43,85,28,84,26" |
请注意,第一个
1 2 3 4 5 6 | char ignore_data[5]; int x1,x2,x3,y1,y2,y3; char str[]="DATA,97,103,100,97,84,69"; sscanf_s(str,"%5[^,],%d,%d,%d,%d,%d,%d", ignore_data, &x1, &x2, &x3, &y1, &y2, &y3); printf("str=%s, x1=%d, x2=%d, x3=%d, x1=%d, y2=%d, y3=%d",str, x1, x2, x3, y1, y2,y3); |
该代码不起作用,并显示以下错误
Unhandled exception at 0x510A06E4 (msvcr110d.dll) in My Application.exe: 0xC0000005: Access violation writing location 0x00870000.
当
我想获取
我发现,如果您能够执行以下操作,则应该能够获得所需的结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <stdio.h> int main() { char ignore_data[5]; int x1, x2, x3, y1, y2, y3; char str[] ="DATA,97,103,100,97,84,69"; sscanf_s(str,"%5[^,],%d,%d,%d,%d,%d,%d", ignore_data, sizeof(ignore_data), &x1, &x2, &x3, &y1, &y2, &y3); printf("str=%s, x1=%d, x2=%d, x3=%d, x1=%d, y2=%d, y3=%d", str, x1, x2, x3, y1, y2, y3); return 0; } |
对于
编辑:
正如Cremno所指出的,它必须是
注意
引用
The
fscanf_s() function is equivalent tofscanf() except that thec ,s , and[ conversion specifiers apply to a pair of arguments (unless assignment suppression is indicated by a
*). The first of these arguments is the same as forfscanf() . That argument is immediately followed in the argument list by the second argument, which has typersize_t and gives the number of elements in the array pointed to by the first argument of the pair. [...]
现在,进入
The
sscanf_s function is equivalent tofscanf_s , except that input is obtained from a string (specified by the arguments ) rather than from a stream. [...]
希望上面的文本足以指出错误,您缺少