且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我无法打开变量文件名称

更新时间:2022-10-15 11:54:30

C-字符串。使用:

  file.open(file_name.c_str()); 

在C ++ 11中,这不再是必需的。添加了 std :: string 的签名。


Is there any reason why Dev C++ is not letting me do file.open(file_name_variable)? I don't understand why it's not letting me open anything but a hardcoded name like file.open("abc.txt") how do get around this? Don't use Dev C++?

here's basically what I have:

int open_file(string file_name){
    ifstream file;
    file.open(file_name);
    if (!file.is_open()){
        return 0;       
    }
    return 1;
}

You need to pass it a c-string. Use:

file.open( file_name.c_str() );

In C++11, this is no longer necessary. A signature that takes std::string was added.