词条 | scanf_s |
释义 | ANSI C中没有scanf_s(),只有scanf(),scanf()在读取时不检查边界,所以可能会造成内存泄露。所以vc++2005/2008中提供了scanf_s(),在调用时,必须提供一个数字以表明最多读取多少位字符。 MSDN中例子: // 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\", result ); printf( "The contents are: %d %f %c %C %s %S\", 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\", result ); wprintf( L"The contents are: %d %f %C %c %hs %s\", i, fp, c, wc, s, ws); return 0; } |
随便看 |
百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。