Reading /proc

This is a discussion on Reading /proc within the Linux Programming forums, part of the Platform Specific Boards category; Hey everyone, i want to read in the output of /proc/loadavg. Because this should be handled as a normal file, ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 02:14 AM
  2. Replies: 7
    Last Post: 02-02-2009, 06:27 AM
  3. Replies: 2
    Last Post: 01-28-2008, 02: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, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21