fopen_s在ubuntu下无法解析

时间:2015-06-28 13:18:24

标签: c++ ubuntu fopen readfile

我想打开文件,所以我在ubuntu下使用fopen_s函数。虽然我#include <stdio.h>在这里阅读http://en.cppreference.com/w/c/io/fopen,但我得到的错误函数未在范围内声明。 请帮助我,我做错了什么,以及如何让它运行?

FILE *fp;
fopen_s(&fp, strFilename.c_str(), "rb");
if (fp == NULL){
    cout << "cannot open " << strFilename.c_str();
    return false;
}

fclose(fp);

2 个答案:

答案 0 :(得分:2)

在Linux上搜索glibc没有发现在Linux上的glibc中实现fopen_s()的证据。

我在POSIX specification中未发现fopen_s()。在我看来,fopen_s()是一个非便携式库函数,仅在Microsoft Windows上实现。

答案 1 :(得分:2)

尝试使用fopen()

#ifdef __unix
#define fopen_s(pFile,filename,mode) ((*(pFile))=fopen((filename),  (mode)))==NULL
#endif

参考:Is there a way to use fopen_s() with GCC or at least create a #define about it?