sscanf_s 在 Visual Studio 2012 中出现错误
Getting error with sscanf_s in Visual Studio 2012
我正在尝试解析以下字符串 - "DATA,97,103,100,97,84,69"
。这个字符串可以有以下变体-
"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
函数解析这些字符串的代码-
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
预处理器一起使用时,上述代码可以完美运行。
我想获取 x1
、x2
、x3
、y1
、y2
和 y3
的值。我在这里错过了什么?
引用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
类型。
我发现如果您能够执行以下操作,您应该能够获得所需的结果:
#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)
应该 return 一个无符号值。
我正在尝试解析以下字符串 - "DATA,97,103,100,97,84,69"
。这个字符串可以有以下变体-
"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
函数解析这些字符串的代码-
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
预处理器一起使用时,上述代码可以完美运行。
我想获取 x1
、x2
、x3
、y1
、y2
和 y3
的值。我在这里错过了什么?
引用C11
标准,附件K,章节§K.3.5.3.2,fscanf_s()
函数,(强调我的)
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. [...]
现在,进入 sscanf_s()
的范围,章节 §K.3.5.3.7
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. [...]
希望以上文字足以指出错误,您缺少格式字符串中存在的 [
所需的第二个参数。您需要提供 ignore_data
的大小,如 rsize_t
类型。
我发现如果您能够执行以下操作,您应该能够获得所需的结果:
#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)
应该 return 一个无符号值。