4

I want to open file so I use fopen_s function under ubuntu. Although I #include <stdio.h> as read here http://en.cppreference.com/w/c/io/fopen, I get error function was not declared in the scope. Please help me what am I doing wrong, and how to make it run?

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

fclose(fp);
beginh
  • 1,133
  • 3
  • 26
  • 36

2 Answers2

4

Try to use fopen()

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

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

Community
  • 1
  • 1
Zhixin Liu
  • 141
  • 2
2

Searching glibc on Linux finds no evidence that fopen_s() is implemented in glibc on Linux.

I find no mention of fopen_s() in the POSIX specification. It appears to me like fopen_s() is a non-portable library function that's implemented only on Microsoft Windows.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • thanks for that research, I did just follow tutorial, but indeed it is on windows. Is there any possibility to make the totally same with different funtion? – beginh Jun 28 '15 at 13:41
  • 1
    It is part of the C11 specification, page 490: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf (with description on page 588) Obviously, the degree of implementation for C11 on Linux, I can't comment on. – Mats Petersson Jun 28 '15 at 13:49
  • 1
    Use fopen() instead of fopen_s(). I do not see anything that fopen_s() does that cannot be accomplished with fopen(). – Sam Varshavchik Jun 28 '15 at 14:04