VS中的scanf问题
#define_CRT_SECURE_NO_WARNINGS
#包含
intmain () )
{
chars1[20]、s2[20]、*p1=s1、*p2=s2;
flush(stdin;
scanf_s('%s ',s1 );
//gets _ s (S1,20 );
for (; *p1!=0; p1,p2 ) {
*p2=*p1;
}
*p2='丰富的月饼/pprintf('%sn ',s2 );
getch (;
返回0;
}
---------解决思路-----------------解决方案
scanf_s是在静态数组中输入值的话,不需要后面的长度。 如果要向字符串指针(动态打开的存储器)输入数据的话,需要后面的长度
---------解决思路-----------------解决方案
宏定义了_CRT_SECURE_NO_WARNINGS。 不用鸟微软的_s函数。
使用_s函数的人检查了多少返回值
---------解决思路-----------------解决方案
调查MSDN是Windows程序员必须掌握的技能之一。
运行时随机参考
scanf_s,_scanf_s_l,wscanf_s,_wscanf_s_l
ExampleSeeAlsoSendFeedback
eadformatteddatafromthestandardinputstream.theseareversionsofscanf,_scanf_l,wscanf,_ wscanf _ lwithsecurityenharithenhanscanforitsttthetttttttestheschescored
intscanf_s (
constchar*format[,
argument]。
);
int_scanf_s_l (
常量字符*格式,
locale_tlocale[,
argument]。
);
intwscanf_s (
constwchar_t*format[,
argument]。
);
int_wscanf_s_l (
constwchar_t*format,
locale_tlocale[,
argument]。
);
参数
格式
格式控制字符串。
协议
最佳化架构。
呼叫
知识产权。
ReturnValue
rturnsthenumberoffieldssuccessfullyconvertedandassigned; thereturnvaluedoesnotincludefieldsthatwerereadbutnotassigned.areturn value of0indicatesthatnofieldswereasigned.the return valun stringcharacterisencounteredinthefirstattempttoreadacharacter.ifformatisanullpointer、 theiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiasdescribedinparametervalidation.ifexecutionisallowedtocontinue
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
wscanf_s, _wscanf_s_l
or
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
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 %dn", result );
printf( "The contents are: %d %f %c %C %s %Sn", 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 %dn", result );
wprintf( L"The contents are: %d %f %C %c %hs %sn", 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.