中的文件時,我嘗試使用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