【发布时间】:2013-11-26 14:26:17
【问题描述】:
在 Windows 上,我从字符串打开文件没有问题。在 Linux 上(它需要工作)我无法打开文件。
string name;
//open 1st file, with the next file name - this works
fstream file( "data.dat", ios::in);
if(file.good()){
getline(file, name);
//some code here
file.close();
}else{
return 1;
}
// this here does not work
fstream file1(name.c_str() , ios::in);
if(file1.good()){
//some code here
file1.close();
}else{
cout<<"can't open file"<<endl;
return 1;
}
如果改为name.c_str()我直接写文件名它可以工作,但每次尝试从文件中获取名称都以文件未打开结束。
我尝试从名称创建 const char*,也不起作用。
【问题讨论】:
-
您是否将
name设置为合理的值?你检查过调试器吗? -
所以您可能没有阅读您认为的内容。也许空间或什么不是。使用调试器或打印出名称。
-
你知道Linux中的文件名是区分大小写的吗?
-
一切都指向
name没有你期望的价值。你查过吗,怎么查的? -
@Eric Finn 是的,我知道,文件名也是小写的。