"Unable to open file", when the program tries to open file in /proc
- by tristartom
Hi,
I try to read file /proc/'pid'/status, using c program. The code is as follows, and even I use sudo to run it, the prompt still keeps throwing "Unable to open file". Please let me know if you have any ideas on how to fix this. thanks
Richard
...
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;
}