I am dealing with a callback function where based on the data in the callback, I want to write to different files.
For example, in one call, I might want to write to january.csv while in another call with different data, it might be july.csv instead. There is no pre-determined sequence, it could be any month in each callback and I have no way of knowing in advance. january.csv (all the months actually) will get written to multiple times.
These callbacks are happening extremely rapidly so I need this code to be as efficient as possible.
The naive approach I would take would be to use the following code each time:
ofstream fout;
fout.open(month_string);
fout<<data_string<<endl;
fout.close();
The problem is that this doesn't seem very efficient since I am continuously opening/closing the month.csv file. Is there a faster way where I can say keep january.csv, february.csv, etc open all the time to make this faster?
EDIT: I am writing to /dev/shm on linux so I/O delays are not really a problem.