I have a program that writes a temporary file to be used with gnuplot. The file varies in size and it can get to several hundreds of kB, if not MB. Everytime it's written to disk, strace
only shows some 8kB at a time. I would like to avoid unnecessary disk writes by setting a buffer greater than this. One of the answers here, on SO, said that 128kB is about the maximum before it starts behaving badly. I have searched and found out that I can modify the buffer, something like this:
int sz {65536};
char buf[sz];
std::ofstream outf {"file.txt"};
outf.rdbuf()->pubsetbuf(&buf[0], sz);
So far, so good, it compiles, but how do I actually use this buffer? In one of the answers, I've seen using reinterpret_cast
, but it I don't really understand what's going on there. The C++ reference site isn't very helpful, either. I am not an advanced programmer, can someone please show me how to use this? I am using ofstream
, and the file written has both data for plotting, and various settings based on conditionals, so I don't know how to fit those in the buffer.