windows中的fopen_s,fprintf_s,fscanf_s等安全增强操作函数
2017-02-22 17:43
447 查看
一 、安全增强函数
VS中进行编程时,经常会遇到 使用fopen不安全,建议使用fopen_s替代的警告:
![](https://img-blog.csdn.net/20170223102050553?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvenh4OTEwNTA5/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
因为 CRT(C运行时库)中的很多函数并不进行参数的越界检测。因此windows提供了对于这些函数的安全增强版本。就是对旧版本加后缀_s ("secure") 。其中有对于文件操作的有fopen_s,fprintf_s,fscanf_s等。还有sprintf_s,strcat_s,strcpy_s等。
这些安全增强函数并没有避免错误。只是在原来的基础上增加了安全检测。在越界时,返回错误码(设置errno值)。
二、 fopen 与fopen_s对比
(1) 函数原型
errno_t fopen_s(
FILE** pFile,
const char *filename,
const char *mode
);
fopen成功调用是返回文件指针;失败时返回NULL
fopen_s成功是返回0;失败时返回错误码
(3)说明
使用fopen_s打开的文件时不可以共享的。
(4)示例
// This program opens two files. It uses
// fclose to close the first file and
// _fcloseall to close all remaining files.
#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' 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 );
}执行结果:
VS中进行编程时,经常会遇到 使用fopen不安全,建议使用fopen_s替代的警告:
因为 CRT(C运行时库)中的很多函数并不进行参数的越界检测。因此windows提供了对于这些函数的安全增强版本。就是对旧版本加后缀_s ("secure") 。其中有对于文件操作的有fopen_s,fprintf_s,fscanf_s等。还有sprintf_s,strcat_s,strcpy_s等。
这些安全增强函数并没有避免错误。只是在原来的基础上增加了安全检测。在越界时,返回错误码(设置errno值)。
二、 fopen 与fopen_s对比
(1) 函数原型
errno_t fopen_s(
FILE** pFile,
const char *filename,
const char *mode
);
FILE *fopen( const char* filename, const char* mode );(2)返回值
fopen成功调用是返回文件指针;失败时返回NULL
fopen_s成功是返回0;失败时返回错误码
pFile | filename | mode | Return Value | Contents ofpFile |
NULL | any | any | EINVAL | unchanged |
any | NULL | any | EINVAL | unchanged |
any | any | NULL | EINVAL | unchanged |
使用fopen_s打开的文件时不可以共享的。
(4)示例
// This program opens two files. It uses
// fclose to close the first file and
// _fcloseall to close all remaining files.
#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' 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 );
}执行结果:
The file 'crt_fopen_s.c' was opened The file 'data2' was opened Number of files closed by _fcloseall: 1 |
相关文章推荐
- c语言中所有文件操作函数详解fopen、fwrite、fread、fgetc、fputc、fscanf、fprintf、ftell、fseek等函数
- c语言中所有文件操作函数详解fopen、fwrite、fread、fgetc、fputc、fscanf、fprintf、ftell、fseek等函数
- c语言中所有文件操作函数详解fopen、fwrite、fread、fgetc、fputc、fscanf、fprintf、ftell、fseek等函数
- 15:文件操作(C标准库函数)fopen/fclose/fread/fwrite/fseek/fprintf/fscanf
- 文件操作——fread,fwrite,fscanf和fprintf函数详解
- C/C++文件操作fopen/fscanf/ fprintf/ fclose简单使用方法
- fopen\fread\fwrite\fscanf\fprintf\fseek\feof\rewind\fgets\fputc等系列函数使用总结
- 文件操作函数 C语言 (FILE fputc fgetc fputs fgets fscanf fprintf)
- fwrite fread fscanf fprintf 及C语言对字符串操作函数
- fopen\fread\fwrite\fscanf\fprintf\fseek\feof\rewind\fgets\fputc等系列函数使用总结
- fopen\fread\fwrite\fscanf\fprintf\fseek\feof\rewind\fgets\fputc等系列函数使用总结
- 文件操作函数 C语言 (FILE fputc fgetc fputs fgets fscanf fprintf)
- Linux C 文件输入输出函数 fopen()、getc()/fgetc()、putc()/fputc()、fclose()、fprintf()、fscanf()、fgets()、fputs()、fseek()、ftell()、fgetpos()、fsetpos() 详解
- C语言几个文件操作函数fopen(),fseek(),fscanf(),fwrite()
- 文件操作 freopen fopen FILE* fscanf fprintf fclose remove rename fseek
- C++笔记 | C fopen函数 fwrite函数 rewind fseek fscanf fprintf等文件操作函数
- 关于安全字符串操作函数
- 如何关闭windows 2003中ie增强的安全配置
- Windows文件操作的直接函数调用
- C语言操作WINDOWS系统存储区数字证书相关函数详解及实例