我无法打开变量文件名

cal*_*pto 0 c++ file-io file

有什么理由Dev C++不让我这么做file.open(file_name_variable)吗?我不明白为什么它不会让我打开任何东西,而是一个硬编码的名字,比如file.open("abc.txt")如何解决这个问题?不要使用Dev C++?

这基本上就是我所拥有的:

int open_file(string file_name){
    ifstream file;
    file.open(file_name);
    if (!file.is_open()){
        return 0;       
    }
    return 1;
}
Run Code Online (Sandbox Code Playgroud)

Ben*_*ley 6

你需要传递一个c字符串.使用:

file.open( file_name.c_str() );
Run Code Online (Sandbox Code Playgroud)

在C++ 11中,不再需要它.std::string添加了一个签名.