我无法打开变量文件名称 [英] I can't open variable file name

查看:108
本文介绍了我无法打开变量文件名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

基本上就是我所拥有的:

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


解决方案

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.

这篇关于我无法打开变量文件名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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