首页 > 编程知识 正文

fopen与open的区别,fopen_s和fopen的区别

时间:2023-05-03 11:21:16 阅读:251217 作者:258

fopen:

原型:FILE * fopen(const char * path,const char * mode);接收两个实参

返回值:文件顺利打开后,指向该流的文件指针就会被返回。如果文件打开失败则返回NULL,并把错误代码存在errno 中。

示例程序源码:FILE *cfPtr;

                         if((cfPtr = fopen("test.dat","w")) == NULL)   //若cfPtr = NULL,即文件未成功打开,函数返回0,否则返回1

                                   return 0;

                         else   return 1;

fopen_s:

原型:

errno_t fopen_s( FILE** pFile, const char *filename, const char *mode ); errno_t _wfopen_s( FILE** pFile, const wchar_t *filename, const wchar_t *mode ); pFile:文件指针将接收到打开的文件指针指向的指针。infilename:文件名。inmode:允许的访问类型。

返回值:如果成功返回0,失败则返回相应的错误代码。

示例程序源码:(转载自:百度百科)

#include<stdio.h>FILE*stream,*stream2;intmain(void){errno_terr;//Openforread(willfailiffile"crt_fopen_s.c"doesnotexist)err=fopen_s(&stream,"crt_fopen_s.c","r");if(err==0){printf("Thefile'crt_fopen_s.c'wasopenedn");}else{printf("Thefile'crt_fopen_s.c'wasnotopenedn");} //Openforwriteerr=fopen_s(&stream2,"data2","w+");if(err==0){printf("Thefile'data2'wasopenedn");}else{printf("Thefile'data2'wasnotopenedn");}//ClosestreamifitisnotNULLif(stream){err=fclose(stream);if(err==0){printf("Thefile'crt_fopen_s.c'wasclosedn");}else{printf("Thefile'crt_fopen_s.c'wasnotclosedn");}}//Allotherfilesareclosed:intnumclosed=_fcloseall();printf("Numberoffilesclosedby_fcloseall:%un",numclosed);}


Git-查找回购中哪些应用已更改

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。