1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Print current status to file every x seconds? (C)

Discussion in 'HTML, Graphics & Programming' started by FreeStream, 1 Jan 2011.

  1. FreeStream

    Capodecina

    Joined: 10 Apr 2004

    Posts: 13,383

    Im running an app simulating a aircraft in flight, and thus it has numerous variables being calculated and so on.

    As it is running a timestep interval of 0.01 seconds, in a 7 hour "flight" that's alot of data (About 50.4 million pieces!)

    I'd like to write certain variables (speed, altitude, etc) to a file so I can import it to excel and plot a graph of it.

    How can I tell it to do this every say, second? Or 10 seconds?

    An if statement like so:

    if (time = integer){
    fprintf=...
    }

    But how do I go about the if (time=multiple of 1/10/100?)

    Also, as the flight is broken up into individual sections (takeoff, landing, cruise) I assume I just amend the file, not create a new one? Using "a" on fopen?

    Cheers :)
     
  2. tntcoder

    Mobster

    Joined: 7 Apr 2004

    Posts: 4,212

    Sticking to pure C, you can populate a time_t structure as follows below. The time_t variable should be integer representing epoch time, i.e number of seconds since 1970.

    Code:
    #include <time.h>
    
    ...
    
    time_t start_time = time(NULL); // get the start time
    
    ...
    
    // Check to see if 10 seconds has gone
    if( time(NULL) - start_time >= 10)
    {
        // output to file
        start_time = time(NULL); // reset the timer
        // keep going -> repeat
    }
    else
    {
       // keep going -> repeat
    }
    
    Is that the sort of thing you wanted?

    And yes, you want to open the file in append mode :)
     
  3. Hamish

    Mobster

    Joined: 14 Apr 2003

    Posts: 4,951

    Location: Deepest Yorkshire

    for this you can use the modulo operator to see the remainder of the interger division opteration. If it's 0 then the amount divides exactly.

    syntax something like
    If ((time % 100) == 0) {
    // time is divisible by 100
    }
     
  4. Yakyb

    Gangster

    Joined: 8 Sep 2009

    Posts: 394

    Question, how do you plan on importing 50 million records into excel? the number of Rows in 2007 is a little over 1 million also there is a maximum number of records excel can graph (32000).

    i would consider the use of a DataBase and a Charting utility

    in terms of inputting data on a regular basis i know c# can use the ontick event.

    it may be worth caching the data and doing Less frequent Large writes than very often small writes (but im just guessing)

    meaning that you could call a number of public variables in the ontick and write them to a list once the list grows to a certain size (30 seconds?) you can write that list to your DB
     
    Last edited: 2 Jan 2011
  5. FreeStream

    Capodecina

    Joined: 10 Apr 2004

    Posts: 13,383

    Perfect, that was what I was looking for!

    Edit: Xcode complains that it is an invalid operands to binary%?
    Edit 2: It seems to be complaining that my variable is a float (Well duh!) and the fact I comparing it to an int :/
    Edit 3: From more reading this way won't work unless I can convert the float into an int (Perhaps by multiplying it by 1/timestep = 100?)


    I'm not, if I was to import every 0.01 second step then yes, it would be too big.

    But every 1 second drops down to just 25,200 lines and 500,000 cells of data. (Only interested in 200,000 of them however).

    Also, how do I go about caching it? Currently they are just displayed in Terminal (cmd for you windows people!) which uses a rather staggering 1.5gb of memory :o

    Edit 4: tntcoder- might give that a bash actually...

    Edit 5: Not perfect the way i've done it (Simular to your tntcoder but didn't bother using the time thing, just set the starttime and keep setting it once it hits >=1.) But it works!

    Cheers :)
     
    Last edited: 2 Jan 2011