fopen和fopen_s用法的比较 【zz】
在定义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多了溢出检测,更安全一些。(在以后的文章里还有get与get_s的比较,strcpy strcpy_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' 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 );
}
原文:http://blog.163.com/lby147612@126/blog/static/1704104522011111452835960/
fopen和fopen_s用法的比较 【zz】的更多相关文章
- fopen和fopen_s用法的比较
open和fopen_s用法的比较 fopen 和 fopen_s fopen用法: fp = fopen(filename,"w"). fop ...
- unix fopen和fopen_s用法的比较
在定义FILE * fp 之后,fopen的用法是: fp = fopen(filename,"w").而对于fopen_s来说,还得定义另外一个变量errno_t err,然后e ...
- 【C++】fopen与fopen_s
说明: VS2010中使用fopen,是没有问题的.使用VS2015时由于VS的高版本对文件操作的安全性有了较高的要求,所以会出现如下情况: fopen用法: fp = fopen(filename ...
- fopen和fopen_s的区别
转载:https://blog.csdn.net/keith_bb/article/details/50063075 fopen: 原型:FILE * fopen(const char * path, ...
- 关于typedef的用法总结(zz)
不管实在C还是C++代码中,typedef这个词都不少见,当然出现频率较高的还是在C代码中.typedef与#define有些相似,但更多 的是不同,特别是在一些复杂的用法上,就完全不同了,看了网上一 ...
- UVM phase的用法研究【zz】
原文地址:http://bbs.eetop.cn/viewthread.php?tid=383872&extra=&authorid=828160&page=1 我相信很多朋友 ...
- fopen()和fclose()的用法
fopen()和fclose()的用法 1.fopen()函数的用法 fopen函数用于打开文件, 其调用格式为: FILE *fopen(char *filename, *type); fopen( ...
- Testbench学习——$fopen/$display/$fclose
昨天在用Vivado写Testbench顶层时,为了以后便于数据的存储导出分析,需要用的文件数据记录的功能,于是,下面谈谈$fopen/$display/$fclose这三者的用法. $fopen—— ...
- fopen()和fclose()
1.fopen()函数的用法fopen函数用于打开文件, 其调用格式为:FILE *fopen(char *filename, *type);fopen()函数中第一个形式参数表示文件名, 可以包含路 ...
随机推荐
- mysql常用命令(1)
Mysql安装目录数据库目录/var/lib/mysql/配置文件/usr/share/mysql(mysql.server命令及配置文件)相关命令/usr/bin(mysqladmin mysqld ...
- 关于C#中的new的用法
修饰符:隐藏基类中的成员(是基类中的成员,所以字段.属性.事件等等都可以隐藏,不单单是方法哦) public class Car { public void WriteName(string name ...
- http接口测试——Jmeter接口测试实例讲解
摘要: 最近做的项目需要测试很多接口,上网查一查,发现完整讲述接口测试的资料太少,所以最近自己做完这个项目,把测试的东西整理一下和大家分享一下,希望对看到的人有所帮助 一.测试需求描述 1. ...
- js typeof
var message = "some thing"; alert(typeof message); // string alert(typeof 95); // number a ...
- android音乐播放器(Service+ContentProvider+Broadcast+Activity四大组件完成)
1.获取音乐 1-1:获取手机中的音乐(用ContentProvider内容提供者来完成): package com.firefly.util; import java.util.ArrayList; ...
- Zabbix安装图解教程
说明: 操作系统:CentOS IP地址:192.168.21.127 Web环境:Nginx+MySQL+PHP zabbix版本:Zabbix 2.2 LTS 备注:Linux下安装zabbix需 ...
- ZIP等
file命令可以查看文件的类型 tar类型 .targzip类型 .gz bzip2类型 .bz2zip类型 .zip 如果一个压缩文件由tar命令解压的前提,2个条件1.这个文件必须是由tar打包之 ...
- HQL基础Query简单查询结果for输出和Iterator输出
HQL第一次课: hibernate Query Language:hibernate 查询语言 语法: query: String hql="from dept"; Query ...
- STM32学习笔记(八) SPI总线(操作外部flash)
1. SPI总线简介 SPI全称串行外设接口,是一种高速,全双工,同步的外设总线:它工作在主从方式,常规需要至少4根线才能够正常工作.SPI作为基本的外设接口,在FLASH,EPPROM和一些数字通讯 ...
- POI导出excel的简单demo
目前使用过两种导出excel的方式,一种是如题所示的使用POI的方式进行数据的导出,这种方式一般只有在处理比较多的数据或者说需要导出的excel表格中有图片之类的需要特殊处理的文件的时候使用:还有一种 ...