C ++类型的参数ifstream :: open() [英] C++ type of argument to ifstream::open()

查看:1591
本文介绍了C ++类型的参数ifstream :: open()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

必须使用什么类型的文件名作为 ifstream.open()

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:


推荐答案

构造函数使用一个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:

in.open(argv[1]);

或者如果您真的要使用文件字符串变量,

or if you really want to use the file string variable, then

in.open(file.c_str());

这篇关于C ++类型的参数ifstream :: open()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆