关于C#:在Visual Studio 2012中使用sscanf_s获取错误

Getting error with sscanf_s in Visual Studio 2012

我正在尝试解析以下字符串-"DATA,97,103,100,97,84,69"。该字符串可以具有以下变体-

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"

请注意,第一个"DATA,"永不更改。其余整数从0到200不等。以下是使用sscanf_s函数解析这些字符串的代码-

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.

sscanf()#define _CRT_SECURE_NO_WARNINGS预处理程序一起使用时,上面的代码可以完美地工作。

我想获取x1x2x3y1y2y3的值。我在这里想念什么?


我发现,如果您能够执行以下操作,则应该能够获得所需的结果:

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;
}

对于sscanf_s,当接收字符串时,在扫描数据后立即需要目标缓冲区的大小。这有助于在输入流(无论可能是什么)与目的地之间建立一个安全端口,从而减少了在接受字符串时缓冲区溢出的可能性。

编辑:

正如Cremno所指出的,它必须是sscanf_s(str,"%5[^,],%d,%d,%d,%d,%d,%d", ignore_data, (unsigned)sizeof(ignore_data), &x1, &x2, &x3, &y1, &y2, &y3);

注意sizeof(ignore data)应该返回无符号值的事实。


引用C11标准,附件K,第§K.3.5.3.2章,fscanf_s()函数,(强调我的)

The fscanf_s() function is equivalent to fscanf() except that the c, 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 for fscanf(). That argument is immediately followed in the argument list by the second argument, which has type rsize_t and gives the number of elements in the array pointed to by the first argument of the pair. [...]

现在,进入sscanf_s()的范围,第§K.3.5.3.7

The sscanf_s function is equivalent to fscanf_s, except that input is obtained from a string (specified by the argument s) rather than from a stream. [...]

希望上面的文本足以指出错误,您缺少[所需的第二个参数,该参数以格式字符串形式出现。您需要提供ignore_data的大小,作为rsize_t类型。