C++ ifstream::open() 的参数类型
我必须将文件名设置为什么类型才能将其用作 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;
...
使用此代码,我收到以下错误:
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>]|
更新:
我收到此错误
What type must I make my file name to use it as an argument to 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;
...
With this code, I get the following error:
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>]|
UPDATE:
i get this error
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
构造函数采用 const char* (http://www.cplusplus.com/reference/iostream/ifstream/ifstream/) 所以你应该这样做:
或者如果你真的想使用文件字符串变量,那么
The constructor takes a const char* (http://www.cplusplus.com/reference/iostream/ifstream/ifstream/) so you should do it like this:
or if you really want to use the file string variable, then