在定义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. Prime Query (ZOJ 3911 线段树)

    Prime Query Time Limit: 1 Second Memory Limit: 196608 KB You are given a simple task. Given a sequen ...

  2. U3D使anim,unity,prefab文件不显示乱码

    Edit-Project Setting-Editor-Asset Serialization-mode Force Text

  3. NSDate获取当前时区的时间

    [NSDate date]获取的是GMT时间,要想获得某个时区的时间,以下代码可以解决这个问题 NSDate *date = [NSDate date]; NSTimeZone *zone = [NS ...

  4. [问题2014A01] 复旦高等代数 I(14级)每周一题(第三教学周)

    [问题2014A01]  试求下列 \(n\) 阶行列式的值: \[ |A|=\begin{vmatrix} 1 & x_1(x_1-a) & x_1^2(x_1-a) & \ ...

  5. 十五、JDBC操作数据库

    1.数据库特点 实现数据共享.减少数据的冗余度.数据的独立性.数据集中控制.数据的一致性和可维护性. 2.数据库种类和功能 (1)层次型数据库:类似于树结构,是一组通过链接而互相联系在一起的记录. ( ...

  6. Cheatsheet: 2015 08.01 ~ 08.31

    Java Beginner's Guide to MVC with Spring Framework Exploring the Spring Web MVC for Web Application ...

  7. shell中 "" 跟 ''的区别

    在bash里,这两个都是引号,用来表明字符串,区别是,双引号中的变量会被展开,而单引号中不再展开.举个例子:a="abc"echo "str=$a" # 结果显 ...

  8. Java开发中经典的小实例-(能被3整除,并且十个数换一行)

    import java.util.Scanner;public class Test15 {    public static void main(String[] args) {        // ...

  9. consul的安装配置 一centos7环境

    centos7上consul的安装--新手笔记 环境 我用的是centos7, 用的是vmware 一 安装系统后首先要设置ip ifconfig eth0 →查看IP 不过输出的信息多一些 ifco ...

  10. NetBeans IDE配置调试

    1.首先需要下载xdebug,在网址中http://www.xdebug.org/download.php可据实际配置环境下载,当然有的Apache安装后已经安装该项,就不必作处理: 2.配置php. ...