fopen 与 fopen_s

来源:互联网 发布:淘宝客服搞笑语录 编辑:程序博客网 时间:2024/05/17 23:58
FILE * fopen(const char *file, const char *mode)  {      return( _fsopen(file, mode, _SH_DENYNO) );  //_SH_DENYNO允许共享读写操作}    errno_t fopen_s(FILE ** pfile, const char *file, const char *mode)  {      _VALIDATE_RETURN_ERRCODE((pfile != NULL), EINVAL);      *pfile = _fsopen(file, mode, _SH_SECURE);  //_SH_SECURE写独占,多进程导致打开文件句柄为空      if(*pfile != NULL)          return 0;      return errno;  }  #define _SH_DENYRW      0x10    /* deny read/write mode */  #define _SH_DENYWR      0x20    /* deny write mode */  #define _SH_DENYRD      0x30    /* deny read mode */  #define _SH_DENYNO      0x40    /* deny none mode */  #define _SH_SECURE      0x80    /* secure mode */  



上面的fopen 与 fopen_s可以看到,其实这两个函数是有本质区别的,之前将fopen全部替换成fopen_s导致进程立刻崩溃连vs调试器都无法中断下来就是因为没有检查返回句柄是否为空(为什么返回空,在多进程同时操作一个文件的时候会导致另外一个进程无法打开此文件),fprintf 调用后进程立刻退出!

原创粉丝点击