scanf_s,在ANSIC中是沒有的,只有scanf(),scanf()在讀取時(shí)不檢查邊界,所以可能會造成內(nèi)存泄露。
ANSI C中沒有scanf_s(),只有scanf(),scanf()在讀取時(shí)不檢查邊界,所以可能會造成內(nèi)存泄露。所以vc++2005/2008中提供了scanf_s(),在調(diào)用時(shí),必須提供一個(gè)數(shù)字以表明最多讀取多少位字符。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 %dn", result );
printf( "Thecontentsare: %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);
return 0;
}

愛華網(wǎng)本文地址 » http://www.klfzs.com/a/8103290103/46691.html
愛華網(wǎng)


