中的文件时,我尝试使用c程序读取文件/ proc /'pid'/ status。代码如下,即使我使用sudo来运行它,提示仍会一直抛出“无法打开文件”。请让我知道如果你有任何想法如何解决这个问题。感谢“无法打开文件”,当程序尝试打开/ proc
理查德
...
int main (int argc, char* argv[]) {
string line;
char* fileLoc;
if(argc != 2)
{
cout << "a.out file_path" << endl;
fileLoc = "/proc/net/dev";
} else {
sprintf(fileLoc, "/proc/%d/status", atoi(argv[1]));
}
cout<< fileLoc << endl;
ifstream myfile (fileLoc);
if (myfile.is_open())
{
while (! myfile.eof())
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
@tristartom:试过打印'errno'? – Potatoswatter 2010-04-06 04:14:44