【发布时间】:2011-06-10 19:14:32
【问题描述】:
这是我在实现方法之前用来测试方法的主文件。我正在尝试获取目录中所有文件的列表,将它们写入 txt 文件(直到这里都可以正常工作),然后从该文本文件中读取文件名。
using namespace std;
string sysCall = "", location = "~/Documents/filenames.txt";
string temp = "";
sysCall = "ls / > "+location;
system(sysCall.c_str());
ifstream allfiles(location.c_str());
allfiles.good();
getline(allfiles, temp);
cout<<temp<<endl; //At this point, the value of temp is equal to ""
return -1;
程序运行后,没有任何文字输出。从我在其他人的问题中读到的内容来看,这应该有效(但显然无效)。我在这里做错了什么?
编辑:allfiles.good() 返回 false,但我不明白为什么它会返回...
【问题讨论】:
-
文件
~/Documents/filenames.txt在您运行程序后是否包含预期的文件列表,即是否只有读取出错? -
另外,像这样单独调用
allfiles.good()并没有任何作用。您应该检查allfiles.good()是返回true还是false。如果是false,说明文件打开不正确,文件名有问题。 -
是的,filenames.txt 文件包含预期文件名的列表。只有阅读它们才是问题
标签: c++ ifstream getline system-calls