"Unable to open file", when the program tries to open file in /proc

Posted by tristartom on Stack Overflow See other posts from Stack Overflow or by tristartom
Published on 2010-04-06T03:42:54Z Indexed on 2010/04/06 3:53 UTC
Read the original article Hit count: 323

Filed under:
|
|

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;  
}  

© Stack Overflow or respective owner

Related posts about c

    Related posts about file-io