Question

void FileIO :: openFile(const char* m_FileName,const char* m_FileMode);

I am getting error:

FileIO.cpp: In static member function ‘static void FileIO::openFile(const char*, const char*)’:
FileIO.cpp:12:45: error: no matching function for call to ‘std::basic_ifstream<char>::open(const char*&, const char*&)’
FileIO.cpp:12:45: note: candidate is:
In file included from FileIO.h:1:0:
/usr/include/c++/4.7/fstream:531:7: note: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
/usr/include/c++/4.7/fstream:531:7: note:   no known conversion for argument 2 from ‘const char*’ to ‘std::ios_base::openmode {aka std::_Ios_Openmode}’
Was it helpful?

Solution

std::basic_ofstream::open doesn't take two const char*s. (note: your subject says ofstream but from your comments it appears you're talking about ifstream).

http://en.cppreference.com/w/cpp/io/basic_ifstream/open

void open( const char *filename,
           ios_base::openmode mode = ios_base::in );
void open( const std::string &filename,                                  
           ios_base::openmode mode = ios_base::in ); (since C++11)

The problem is the second, not the first argument.

ifstream ifs;
ifs.open("hello", "rb" /*<-- problem, this is a const char* not flags.*/);

Instead, you need to pass it std::ios_base flags

ifstream ifs("hello", std::ios_base::in | std::ios_base::binary);

or

ifstream ifs;
ifs.open("hello", std::ios_base::in | std::ios_base::binary);

--- EDIT ---

Looking at your comments following the post (why didn't you edit the post?) you are also trying to check for 'NULL'.

In C and C++ 'NULL' is a macro which is #defined as 0. So, checking for NULL can check for a null pointer, but it also can test for numeric values. If you want to check if the file opened, you will need to do:

m_FileInput.open("hello", std::ios_base::in | std::ios_base::binary);
if (!m_FileInput.good()) // checks if the file opened.

And you should try to use 'nullptr' instead of 'NULL' when possible.

OTHER TIPS

You're trying to use C's FILE* syntax to call a C++ open function. The mode (read/write/append) arguments are NOT string literals in C++ but enumerated values possibly ORed together.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top