Thread: How can we achive this?

  1. #1
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732

    How can we achive this?

    Hi guys, just a quick about fclose or close function. What i am after is to log all the error messges on to a file. But I dont want that file to be written to the hard disk, instead I want this be on the RAM.

    The reason is beacause, i dnt have much flash memory on my board. It has just got 16MB flash. And if I keep on logging message , there would a time where it will run out. What I thought of is, since I have lots of primary memory like say 64MB of RAM and board will be on all the time the data will be saved there till the power is supplied.

    When power cuts of well you could say the board has been resetted. So for example

    Code:
    int main()
    {
       ...
       fopen();
       fputs(...);
       fputs(...);
     
       ...
       return 0;
    }
    It looks like when I close the file, all the buffered data which i unwrittem to the file be written. Am I right? Or if I dont close the file will all data be in the RAM? As far as i am aware when it reaches main all data will be written to main.

    Thanks a lot!

    ssharish
    Last edited by ssharish2005; 09-18-2007 at 04:50 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Use stderr in the program, and whenever you run your executable, redirect that stream to a log file.

    $ exec 2> errors.log

    The shell will close the file when the program terminates.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Citizen thanks for your reply, But still in your case
    >The shell will close the file when the program terminates.
    When the file gets closed you are writting the contents of the file to the flash, eating up the memory.

    What happens if the I am keep on writting, thats like there is a process which just keeps on written as a backgroud process which will never close the file. But I guess once the buffere gets filled it writtes it to the file.

    Or is it just the case of writting on to a string?

    ssharish
    Last edited by ssharish2005; 09-18-2007 at 05:18 PM.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    54
    If whatever thing you're running this on uses Linux or another unix variant, you can put the login /tmp/ and it will be saved in memory (the /tmp/ dir is a part of the filesystem contained in memory).
    Last edited by Doodle77; 09-18-2007 at 05:36 PM.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by Doodle77 View Post
    If whatever thing you're running this on uses Linux or another unix variant, you can put the login /tmp/ and it will be saved in memory (the /tmp/ dir is a part of the filesystem contained in memory).
    You just gave me a good point. what ever witten on tmp is till on RAM the contents of the folder will be deleted when the ssystem restarts. And I have upto 4MB file system mounted to tmp directory. hmm thats would help a bit.

    Thanks a lot

    ssharish

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You could also use some sort of circular log. After the log file gets, say, 100 messages, you delete the first line before adding another one, so that the log file never has more than 100 lines in it.

    Just a thought.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how can I achive when, do?
    By Subsonics in forum C Programming
    Replies: 2
    Last Post: 03-21-2009, 03:07 AM
  2. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM