scanf_s

王朝百科·作者佚名  2010-12-08
窄屏简体版  字體: |||超大  

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;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航