在定义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】的更多相关文章

  1. fopen和fopen_s用法的比较

    open和fopen_s用法的比较 fopen 和 fopen_s           fopen用法: fp = fopen(filename,"w").         fop ...

  2. unix fopen和fopen_s用法的比较

    在定义FILE * fp 之后,fopen的用法是: fp = fopen(filename,"w").而对于fopen_s来说,还得定义另外一个变量errno_t err,然后e ...

  3. 【C++】fopen与fopen_s

    说明: VS2010中使用fopen,是没有问题的.使用VS2015时由于VS的高版本对文件操作的安全性有了较高的要求,所以会出现如下情况: fopen用法:  fp = fopen(filename ...

  4. fopen和fopen_s的区别

    转载:https://blog.csdn.net/keith_bb/article/details/50063075 fopen: 原型:FILE * fopen(const char * path, ...

  5. 关于typedef的用法总结(zz)

    不管实在C还是C++代码中,typedef这个词都不少见,当然出现频率较高的还是在C代码中.typedef与#define有些相似,但更多 的是不同,特别是在一些复杂的用法上,就完全不同了,看了网上一 ...

  6. UVM phase的用法研究【zz】

    原文地址:http://bbs.eetop.cn/viewthread.php?tid=383872&extra=&authorid=828160&page=1 我相信很多朋友 ...

  7. fopen()和fclose()的用法

    fopen()和fclose()的用法 1.fopen()函数的用法 fopen函数用于打开文件, 其调用格式为: FILE *fopen(char *filename, *type); fopen( ...

  8. Testbench学习——$fopen/$display/$fclose

    昨天在用Vivado写Testbench顶层时,为了以后便于数据的存储导出分析,需要用的文件数据记录的功能,于是,下面谈谈$fopen/$display/$fclose这三者的用法. $fopen—— ...

  9. fopen()和fclose()

    1.fopen()函数的用法fopen函数用于打开文件, 其调用格式为:FILE *fopen(char *filename, *type);fopen()函数中第一个形式参数表示文件名, 可以包含路 ...

随机推荐

  1. Arcgis Server 默认服务端口号修改方法

    本人安装Arcgis Server 10.2之后发布了一个地图服务,该服务默认使用的端口号是6080,本人使用的是教育网,使用教育网均能正常使用该服务,但是使用电信或者移动网络均不能正常访问该网站. ...

  2. jQuery : eq() vs get()

    .get(index) and .eq(index) both return a single "element" from a jQuery object array, but ...

  3. Java通过继承thread类与实现Runnable接口实现多线程的区别

    Java中线程的创建有两种方式: 1.  通过继承Thread类,重写Thread的run()方法,将线程运行的逻辑放在其中 2.  通过实现Runnable接口,实例化Thread类 一.通过继承T ...

  4. Orcale与jfinal的添加时间问题

    时间只能不能使用一般的方法进行添加,必须转换,比如添加当前时间,如上图所示

  5. string类find函数返回值判定

     string类find函数返回值判定 代码示例 #include<iostream> #include<cstring> using namespace std; int m ...

  6. sql按时间段汇总

    select dateadd(mi,(datediff(mi,convert(varchar(10),dateadd(ss,-1,CreateOn),120),dateadd(ss,-1,Create ...

  7. [Python]处理windows下多级目录文件,上传到Linux服务器

    #-*- coding: utf-8 -*- __author__ = 'tsbc' import sys reload(sys) sys.setdefaultencoding('utf-8') im ...

  8. Mysql常用数据类型

    Mysql常用数据类型 数字: 字符串: 时间:

  9. 浏览器URL编码

    jsp页面中通过请求另一个页面并通过url传递了带有中文的参数,结果在接收端获取参数时乱码了 经检查乱码现象指出新在IE浏览器中,其他浏览器火狐.chrome等不会有问题 最后的解决方式是: 手动将此 ...

  10. sql 、linq、lambda 查询语句的区别

    LINQ的书写格式如下: from 临时变量 in 集合对象或数据库对象 where 条件表达式 [order by条件] select 临时变量中被查询的值 [group by 条件] Lambda ...