VS中scanf的问题

三断笛 2015-09-07 12:07:13
不废话,上代码。刚使用VS2013教小舅子写C,从键盘输入一个字符串,复制到另一个字符串数组。书上照抄的,唯一不同的是,把scanf换成了scanf_s,换了以后scanf_s死活读不到键盘输入的内容(如abc),换成scanf就行,在scanf_s最后加上长度参数也行。按照微软scanf_s的字义,最后一个长度参数是可选的,不应该造成如此大的差别,请大神解释为何scanf_s如此神奇。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
char s1[20], s2[20], *p1 = s1, *p2 = s2;
fflush(stdin);
scanf_s("%s", s1);
//gets_s(s1, 20);
for (; *p1 != 0; p1++, p2++){
*p2 = *p1;

}

*p2 = '\0';
printf("%s\n", s2);
getch();

return 0;
}
...全文
390 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
dustpg 2015-09-07
  • 打赏
  • 举报
回复
都宏定义_CRT_SECURE_NO_WARNINGS了,不用鸟微软的_s函数, 用_s函数的人有多少检查了返回值
二班的码农 2015-09-07
  • 打赏
  • 举报
回复
scanf_s如果是给静态数组输入值就不需要后面的长度,如果是给字符串指针(动态开辟的内存)输入数据就需要后面的长度
赵4老师 2015-09-07
  • 打赏
  • 举报
回复
查MSDN是Windows程序员必须掌握的技能之一。 Run-Time Library Reference scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l Example See Also Send Feedback Read formatted data from the standard input stream. These are versions of scanf, _scanf_l, wscanf, _wscanf_l with security enhancements as described in Security Enhancements in the CRT. int scanf_s( const char *format [, argument]... ); int _scanf_s_l( const char *format, locale_t locale [, argument]... ); int wscanf_s( const wchar_t *format [, argument]... ); int _wscanf_s_l( const wchar_t *format, locale_t locale [, argument]... ); Parameters format Format control string. argument Optional arguments. locale The locale to use. Return Value Returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error or if the end-of-file character or the end-of-string character is encountered in the first attempt to read a character. If format is a NULL pointer, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, scanf_s and wscanf_s return EOF and set errno to EINVAL. For information on these and other error codes, see _doserrno, errno, _sys_errlist, and _sys_nerr. Remarks The scanf_s function reads data from the standard input stream stdin and writes the data into the location given by argument. Each argument must be a pointer to a variable of a type that corresponds to a type specifier in format. If copying takes place between strings that overlap, the behavior is undefined. wscanf_s is a wide-character version of scanf_s; the format argument to wscanf_s is a wide-character string. wscanf_s and scanf_s behave identically identically if the stream is opened in ANSI mode. scanf_s doesn't currently support input from a UNICODE stream. The versions of these functions with the _l suffix are identical except that they use the locale parameter passed in instead of the current thread locale. Unlike scanf and wscanf, scanf_s and wscanf_s require the buffer size to be specified for all input parameters of type c, C, s, S, or [. The buffer size is passed as an additional parameter immediately following the pointer to the buffer or variable. For example, if reading a string, the buffer size for that string is passed as follows: char s[10]; scanf("%9s", s, 10); The buffer size includes the terminating null. A width specification field may be used to ensure that the token read in will fit into the buffer. If no width specification field is used, and the token read is too big to fit in the buffer, nothing will be written to that buffer. Note: The size parameter is of type unsigned, not size_t. In the case of characters, one may read a single character as follows: char c; scanf("%c", &c, 1); When reading multiple characters for non-null terminated strings, integers are used as the width specification and the buffer size. char c[4]; scanf("%4c", &c, 4); // not null terminated For more information, see scanf Width Specification. TCHAR.H routine _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined _tscanf_s scanf_s scanf_s wscanf_s _tscanf_s_l _scanf_s_l _scanf_s_l _wscanf_s_l For more information, see Format Specification Fields — scanf functions and wscanf Functions. Requirements Routine Required header scanf_s, _scanf_s_l <stdio.h> wscanf_s, _wscanf_s_l <stdio.h> or <wchar.h> For additional compatibility information, see Compatibility in the Introduction. Example Copy Code // crt_scanf_s.c // This program uses the scanf_s and wscanf_s functions // to read formatted input. #include <stdio.h> int main( void ) { int i, result; float fp; char c, s[81]; wchar_t wc, ws[81]; result = scanf_s( "%d %f %c %C %s %S", &i, &fp, &c, 1, &wc, 1, s, 80, ws, 80 ); printf( "The number of fields input is %d\n", result ); printf( "The contents are: %d %f %c %C %s %S\n", i, fp, c, wc, s, ws); result = wscanf_s( L"%d %f %hc %lc %S %ls", &i, &fp, &c, 2, &wc, 1, s, 80, ws, 80 ); wprintf( L"The number of fields input is %d\n", result ); wprintf( L"The contents are: %d %f %C %c %hs %s\n", i, fp, c, wc, s, ws); } Copy Code 71 98.6 h z Byte characters 36 92.3 y n Wide characters Copy Code The number of fields input is 6 The contents are: 71 98.599998 h z Byte characters The number of fields input is 6 The contents are: 36 92.300003 y n Wide characters .NET Framework Equivalent System::Console::Read System::Console::ReadLine See also Parse methods, such as System::Double::Parse. See Also Concepts Floating-Point Support Stream I/O Locale fscanf, _fscanf_l, fwscanf, _fwscanf_l printf, _printf_l, wprintf, _wprintf_l sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l sscanf, _sscanf_l, swscanf, _swscanf_l Send feedback on this topic to Microsoft.
二班的码农 2015-09-07
  • 打赏
  • 举报
回复
引用 4 楼 xxyj6450 的回复:
引用 1 楼 ant2012 的回复:
scanf_s如果是给静态数组输入值就不需要后面的长度,如果是给字符串指针(动态开辟的内存)输入数据就需要后面的长度
另外,不管我输入啥,它读到的都是0,请问它是从哪读到这个值的?
数据是来自控制台呢
三断笛 2015-09-07
  • 打赏
  • 举报
回复
引用 1 楼 ant2012 的回复:
scanf_s如果是给静态数组输入值就不需要后面的长度,如果是给字符串指针(动态开辟的内存)输入数据就需要后面的长度
另外,不管我输入啥,它读到的都是0,请问它是从哪读到这个值的?
三断笛 2015-09-07
  • 打赏
  • 举报
回复
引用 1 楼 ant2012 的回复:
scanf_s如果是给静态数组输入值就不需要后面的长度,如果是给字符串指针(动态开辟的内存)输入数据就需要后面的长度
哥,我的s1是个静态数组呀,怎么也不行。
赵4老师 2015-09-07
  • 打赏
  • 举报
回复
引用 7 楼 xxyj6450 的回复:
[quote=引用 6 楼 zhao4zhong1 的回复:] 查MSDN是Windows程序员必须掌握的技能之一。.
噢,多谢,感谢大神,原来scanf_s输入字符要求带缓冲大小的参数。 但scanf是完全正常的,我传入scanf_s的变量长度是固定的,%s也明确指示了是要输入字符串,这些信息足够判断指针的长度,因此不应再让我输入它的长度。 而且既然是scanf的高级替代,则应在保留scanf特性的基础上再进行高级的控制,而不应带入这种不一致的副作用。 我对这种设计表示不理解。[/quote] 所以很多人包括我在内都
#pragma warning(disable:4996)
三断笛 2015-09-07
  • 打赏
  • 举报
回复
引用 6 楼 zhao4zhong1 的回复:
查MSDN是Windows程序员必须掌握的技能之一。.
噢,多谢,感谢大神,原来scanf_s输入字符要求带缓冲大小的参数。 但scanf是完全正常的,我传入scanf_s的变量长度是固定的,%s也明确指示了是要输入字符串,这些信息足够判断指针的长度,因此不应再让我输入它的长度。 而且既然是scanf的高级替代,则应在保留scanf特性的基础上再进行高级的控制,而不应带入这种不一致的副作用。 我对这种设计表示不理解。

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧