I’m working in C and I need to write certain output values to a text file. These values will be written to the file at different points of the script.
I’m talking about 50 lines divided in a total of 5 min of execution time.
My script is testing hardware performances (including cpu and disk caches) so it’s very important that nothing slows it down or uses memory.
Is it better to use fopen() and fclose() every time that I want to write a line or just to leave the file open for the entire execution?
You can’t have it both ways – you have to either use memory or use disk cache. Normally, one would save the stats in memory and write them at the end of the test. However, if minimising memory use is what you want, open the file at start of the test and write the stats as you go.