'fopen': This function or variable may be unsafe
来源:互联网 发布:spss for mac下载 编辑:程序博客网 时间:2024/06/06 05:51
在定义FILE* fp 之后,fopen的用法是:fp = fopen(filename,"w")。而对于fopen_s来说,还得定义另外一个变量errno_terr,然后err= fopen_s(&fp,filename,"w")。返回值的话,对于fopen来说,打开文件成功的话返回文件指针(赋值给fp),打开失败则返回NULL值;对于fopen_s来说,打开文件成功返回0,失败返回非0。
在vs编程中,经常会有这样的警告:warningC4996: 'fopen': This function or variable may be unsafe. Consider using fopen_sinstead. To disable deprecation, use_CRT_SECURE_NO_WARNINGS. See online helpfor details. 是因为 fopen_s比fopen多了溢出检测,更安全一些。(在以后的文章里还有get与get_s的比较,strcpystrcpy_s的比较,他们的共同点都是用来一些不可预料的行为,以后将进行详尽解释)
#include <stdio.h>
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' wasnot opened\n" );
else
printf( "The file 'data2' wasopened\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 closedby _fcloseall: %u\n", numclosed );
}
- [Error]'fopen'This function or variable may be unsafe
- error C4996: 'fopen': This function or variable may be unsafe.
- error c4996: 'fopen': This function or variable may be unsafe
- warning C4996: 'fopen': This function or variable may be unsafe
- error c4996: 'fopen': This function or variable may be unsafe
- error C4996: 'fopen': This function or variable may be unsafe.
- error C4996: 'fopen': This function or variable may be unsafe.
- OpenCV--C4996'fopen': This function or variable may be unsafe.
- C4996'fopen': This function or variable may be unsafe.
- 'fopen': This function or variable may be unsafe
- C4996'fopen': This function or variable may be unsafe.解决方案
- This function or variable may be unsafe
- This function or variable may be unsafe
- This function or variable may be unsafe
- This function or variable may be unsafe
- This function or variable may be unsafe
- This function or variable may be unsafe
- This function or variable may be unsafe
- Java内存区域与内存溢出异常整理
- 浅析为什么char类型的范围是-128~+127
- 生物信息类期刊
- curl 如何支持 SSL,https
- JavaScript从入门到放弃(五)
- 'fopen': This function or variable may be unsafe
- Windows消息基本概念及消息系统(第一讲)
- Spring+Apache CXF+MyBatis+Maven实例
- Android 双缓冲绘图机制
- jeckins+(svn,Git)+gradle+android studio
- android SharedPreferences封装类 简单 好用的类
- iOS 手机震动
- convex optimization from stanford
- NYOJ_128 前缀式计算