Nov 20, 2020 09:29 AM
(This post was last modified: Nov 20, 2020 09:36 AM by SIM-888979.)
Hi guys, I'm currently writing a program that will run some calculations then store the data into a .txt file. I do not know how many iterations or data I would have so I cannot use an array to store the data first then process later. So, I decided to call the write to file function every time I do a calculation. This allows me to write up to about 507 lines of data before the buffer becomes full and then refusing to write any more lines. I can have 1 line or I can have 10000 lines in my data, so it is very important I capture all the data.
I know I can fix this by just closing the file using fclose but I also need to keep to a low run time. By not doing fclose(), I cut my runtime from 8 seconds to 0.4 seconds for about 900 lines. What should I do, how do I go about keeping a low run time but still capturing all data in a .txt file?
Fixed! I realized I think each time I called the function, I opened a new file pointer each time? Not too sure. I just decided to use a single file pointer in the main function and parse the pointer to it instead.
I know I can fix this by just closing the file using fclose but I also need to keep to a low run time. By not doing fclose(), I cut my runtime from 8 seconds to 0.4 seconds for about 900 lines. What should I do, how do I go about keeping a low run time but still capturing all data in a .txt file?
Code
void gnuplot(float mae, int iteration) {
FILE *plot_ptr;
plot_ptr = fopen("plotting-data.txt", "a+");
fprintf(plot_ptr, "%i %f\n", iteration, mae);
printf("Write complete.");
}Fixed! I realized I think each time I called the function, I opened a new file pointer each time? Not too sure. I just decided to use a single file pointer in the main function and parse the pointer to it instead.
lel


