Thread: Reading /proc

  1. #1
    Registered User
    Join Date
    Nov 2007
    Location
    Hamburg, Germany
    Posts
    13

    Reading /proc

    Hey everyone,

    i want to read in the output of /proc/loadavg. Because this should be handled as a normal file, i thought i could do it this way (open the files, not yet read them):

    Code:
            char* file = "/proc/loadavg";
    	ofstream output;
    
    	output.open(file,ios_base::in);
    
    	if(output.good()){
    		// was able to open file
    		cout << "opened\n";
    	}
    	
    	output.close();
    	if(output.good()){
    		cout << "closed";
    	}
    But it seems like output.good() and output.close() dont return true, because no output is generated!

    I already checked the rights.

    When I create a text file in my home folder and open it with that code snipped, it works well.

    Any ideas? Google and the board search did not provide any useful results.

    Thank you!
    crisis

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I take it "cat /proc/loadavg" works?

    Can you do "strace cat /proc/loadavg 2> trace.txt"
    Then "strace myprog 2> trace2.txt"

    Compare the open statements for /proc/loadavg and see if there is any difference.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > ofstream output;
    > output.open(file,ios_base::in);
    You're opening an OUTPUT file for INPUT
    What about this seems strange to you?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Location
    Hamburg, Germany
    Posts
    13
    thank you both!

    this works:

    Code:
            ifstream rawavg("/proc/loadavg");
    	string buffer;
    	getline(rawavg,buffer);
    	cout << buffer << "\n";
    	rawavg.close();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-05-2009, 03:14 AM
  2. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  3. Replies: 2
    Last Post: 01-28-2008, 03:07 AM
  4. help with reading from stream
    By movl0x1 in forum C Programming
    Replies: 7
    Last Post: 05-31-2007, 10:36 PM
  5. Reading from a file
    By Wiz_Nil in forum C++ Programming
    Replies: 1
    Last Post: 02-26-2002, 09:31 AM