我必须使用什么类型的文件名作为参数ifstream.open()?
int main(int argc, char *argv[]) {
string x,y,file;
string file = argv[1];
ifstream in;
in.open(file);
in >> x;
in >> y;
...
Run Code Online (Sandbox Code Playgroud)
使用此代码,我收到以下错误:
main.cpp|20|error: no matching function for call to 'std::basic_ifstream<char,
std::char_traits<char> >::open(std::string&)'|
gcc\mingw32\4.4.1\include\c++\fstream|525|note: candidates are: void std::basic_ifstream<_CharT,
_Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]|
Run Code Online (Sandbox Code Playgroud)
更新:
我收到这个错误

构造函数采用const char*(http://www.cplusplus.com/reference/iostream/ifstream/ifstream/),所以你应该这样做:
in.open(argv[1]);
Run Code Online (Sandbox Code Playgroud)
或者如果你真的想使用文件字符串变量,那么
in.open(file.c_str());
Run Code Online (Sandbox Code Playgroud)