-1

I'm beginner and trying to read the file using fopen_s but error shows that

Build: Debug in histimg (compiler: GNU GCC Compiler) ===|

E:\CCS\histimg\main.cpp|19|error: 'fopen_s' was not declared in this scope|

Code:

 fopen_s(&fp,"C:\\Users\\asus\\Pictures\\SavedPictures\\1.png", "rb");
 if (fp == 0)
 {
      cout << "Open image failed!" << endl;
      exit(0);
 }

Can you please guide me how to resolve it. Thanks.

walnut
  • 21,629
  • 4
  • 23
  • 59
MoonAli
  • 13
  • 1
  • 4
  • 1
    Does this answer your question? [Is there a way to use fopen\_s() with GCC or at least create a #define about it?](https://stackoverflow.com/questions/1513209/is-there-a-way-to-use-fopen-s-with-gcc-or-at-least-create-a-define-about-it) – aep Jan 23 '20 at 05:13

2 Answers2

0

fopen_s is not part of C++'s standard library. It is a MSVC-specific extension or optional part of the C standard library.

If your compiler does not support it as an extension, then you cannot use it.

Use the C++ file stream standard library <fstream>, i.e. std::fstream instead.

walnut
  • 21,629
  • 4
  • 23
  • 59
  • Thanks for guidance.But while using std::fstream again got an error. E:\CCS\histimg\main.cpp|19|error: incomplete type 'std::fstream {aka std::basic_fstream}' used in nested name specifier| – MoonAli Jan 23 '20 at 05:40
  • @RizwanAli You need to `#include`. – walnut Jan 23 '20 at 05:49
0

As per https://en.cppreference.com/w/c/io/fopen

As with all bounds-checked functions, fopen_s is only guaranteed to be available if STDC_LIB_EXT1 is defined by the implementation and if the user defines STDC_WANT_LIB_EXT1 to the integer constant 1 before including stdio.h.

Sumit
  • 1,485
  • 11
  • 24
  • The question is about C++, not C. – walnut Jan 23 '20 at 05:15
  • cppreference.com has only that page which mentions fopen_s – Sumit Jan 23 '20 at 05:18
  • Yes, because it is not part of C++ at all. – walnut Jan 23 '20 at 05:18
  • Do you know why OP has mentioned `compiler: GNU GCC Compiler` not g++ ? I am little confused... – Sumit Jan 23 '20 at 05:21
  • "GCC" is used for the whole compiler collection, not only for the C compiler or the `gcc` command specifically. But he surely isn't compiling as C, because `cout <<` would certainly not work in C. – walnut Jan 23 '20 at 05:23
  • but most of the C stuffs work in c++ so if printf is used in a .cpp program then will not the C documentation about printf will be relevant ? – Sumit Jan 23 '20 at 05:24
  • Generally this is true, but C and C++ diverged quite a bit in some regards. You can usually find a C++ and a C version of pages on cppreference.com which will show the information as it applies to C and as it applies to C++. For example [the C `fopen` page](https://en.cppreference.com/w/c/io/fopen) lists `fopen_s`, but [the C++ `fopen` page](https://en.cppreference.com/w/cpp/io/c/fopen) does not. A compiler can of course support other C functions in C++ mode as extension as well. However the C++ standard only mentions about `fopen_s` that it is a reserved name in some contexts. – walnut Jan 23 '20 at 05:27
  • Actually I am not sure that I am completely correct on this. [\[library.headers\]/10](https://eel.is/c++draft/library#headers-10) mentions since C++17 that it is *implementation-defined* whether these functions are (with reference to C11) included in the global namespace when including a C++ header file, but not in the `std` namespace. So I guess in some sense C++ supports these functions in an *implementation-defined* manner. But I think that practically gives the same result. – walnut Jan 23 '20 at 05:39