void FileIO :: openFile(const char* m_FileName,const char* m_FileMode);
我收到错误:
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}’
答案 0 :(得分:4)
std::basic_ofstream::open
不会两个 const char*
。 (注意:您的主题是ofstream
,但是根据您的评论,您似乎在谈论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)
问题是第二个问题,而不是第一个问题。
ifstream ifs;
ifs.open("hello", "rb" /*<-- problem, this is a const char* not flags.*/);
相反,你需要传递它std :: ios_base flags
ifstream ifs("hello", std::ios_base::in | std::ios_base::binary);
或
ifstream ifs;
ifs.open("hello", std::ios_base::in | std::ios_base::binary);
---编辑---
查看帖子后面的评论(为什么不编辑帖子?)你也试图检查'NULL'。
在C和C ++中'NULL'是一个宏#define
d为0.因此,检查NULL
可以检查空指针,但它也是可以测试数值。如果要检查文件是否已打开,则需要执行以下操作:
m_FileInput.open("hello", std::ios_base::in | std::ios_base::binary);
if (!m_FileInput.good()) // checks if the file opened.
如果可能,你应该尝试使用'nullptr'而不是'NULL'。
答案 1 :(得分:0)
您尝试使用C&#39; FILE*
语法来调用C ++ open函数。模式(读/写/追加)参数不是C ++中的字符串文字,但枚举值可能与OR一起。