日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

c语言fopen_s的用法,fopen和fopen_s用法的比较

發布時間:2025/3/8 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c语言fopen_s的用法,fopen和fopen_s用法的比较 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

參考:

在定義FILE * fp 之后,fopen的用法是:?fp?= fopen(filename,"w")。而對于fopen_s來說,還得定義另外一個變量errno_t err,然后err = fopen_s(&fp,filename,"w")。返回值的話,對于fopen來說,打開文件成功的話返回文件指針(賦值給fp),打開失敗則返回NULL值;對于fopen_s來說,打開文件成功返回0,失敗返回非0。

在vs編程中,經常會有這樣的警告:warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use_CRT_SECURE_NO_WARNINGS. See online help for details.是因為? fopen_s比fopen多了溢出檢測,更安全一些。

#include

FILE *stream, *stream2;

int main( void ) { ?? int numclosed; ?? errno_t err;

// Open for read (will fail if file "crt_fopen_s.c" does not exist) ?? if( (err? = fopen_s( &stream, "crt_fopen_s.c", "r" )) !=0 ) ????? printf( "The file 'crt_fopen_s.c' was not opened\n" ); ?? else ????? printf( "The file 'crt_fopen_s.c' was opened\n" );

// Open for write? ?? if( (err = fopen_s( &stream2, "data2", "w+" )) != 0 ) ????? printf( "The file 'data2' was not opened\n" ); ?? else ????? printf( "The file 'data2' was opened\n" );

// Close stream if it is not NULL? ?? if( stream) ?? { ????? if ( fclose( stream ) ) ????? { ???????? printf( "The file 'crt_fopen_s.c' was not closed\n" ); ????? } ?? }

// All other files are closed: ?? numclosed = _fcloseall( ); ?? printf( "Number of files closed by _fcloseall: %u\n", numclosed ); }

總結

以上是生活随笔為你收集整理的c语言fopen_s的用法,fopen和fopen_s用法的比较的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。