Logging > text file - when to open/close V5 VEXCode

We are working on a task to log the motor’s max values for temp and current and looking at this piece of code

However there is concerns about opening & closing files during competition. I am only writing to the file every, say, 10 seconds.
Open file on startup , write as needed - close?? uses less I/O but more memory - have to save file upon completion which IDK how/when to do.

Open / write / close when needed - easy to do but may affect performance?


1 Like

In general it’s good practice to only keep a file open when needed - so if you’re writing some values to a file every 10 seconds, it makes sense to open, write, and close the file every time.

The code you linked does that - ofs.close(); is all you need, the changes are “saved” as soon as you write them.

I doubt the additional load needed to open and close a file every 10 seconds or so will have any noticeable impact on robot performance.

1 Like

cool - thanks for the assurance!

Think it worthwhile to, on start-up, append the log to a 2nd file & clear it (to minimize footprint)?

That depends on what you want to do - you’re probably never going to fill up an SD card of any reasonable size by logging some data every 10 seconds.


Assuming you log 100 bytes every 10 seconds (which seems a bit high but let’s go with it), a 1GB SD card (can you even buy one that small now?) will have enough space for logs from 1157 days of continuous driving.

An easy approach would be to write to a different file (with a different name) each time you start the program. A common approach to this would be to name the file with a timestamp representing when you started the program, but I doubt the V5 brain keeps global time so another approach might be to name the files sequentially.

1 Like

Thanks for the good feedback.

I wasn’t worried about filling the SD as much as affecting up the CPU with I/O calls.

The sequential naming sounds intriguing