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

Soldato
Joined
10 Apr 2004
Posts
13,489
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 a lot 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 :)
 
Soldato
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 :)
 
Soldato
Joined
14 Apr 2003
Posts
4,951
Location
Deepest Yorkshire
But how do I go about the if (time=multiple of 1/10/100?)

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
}
 
Associate
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:
Soldato
OP
Joined
10 Apr 2004
Posts
13,489
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
}

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?)


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

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:
Back
Top Bottom