【发布时间】:2016-05-18 17:57:18
【问题描述】:
我有一个目录,当我在其上运行 ls 命令时,我知道它会吐出如下错误:
ls: reading directory /mydir: Input/output error
我希望能够检测到我的代码中存在 IO 错误。
这是我尝试过的:
void readLs(const std::string& name)
{
stringstream ss;
ss << "ls " << name;
FILE* apipe = popen(ss.str().c_str(), "r");
if(apipe == NULL)
{
cout << "Error opening popen" << endl;
return;
}
char line[256];
cout << __FILE__ << " " << __LINE__ << endl; //This is line 46
while ( fgets( line, 256 , apipe) )
{
string temp(line);
cout << "This is line: " << temp << endl;
memset(line, 0, 256);
}
cout << __FILE__ << " " << __LINE__ << endl; //This is line 53
pclose(apipe);
}
test.cpp 46
ls: reading directory /mydir: Input/output error
test.cpp 53
错误消息打印到屏幕上,但从管道读取时我没有收到错误消息。
谢谢,
【问题讨论】:
-
完美!非常感谢,@antlersoft