c++ - Keeping an ofstream object open even after the function exits -
Visual Studio has a code written in C ++, which basically writes a set of floating point number (70 floating point). A binary file on each ticker of a timer. The number is taken from a machine that is used.
WriteToFile (float * buffer, et buffers) {offstream out; For out.open (filename, ios :: app) (int i = 0; i & lt; buffersize; i ++) // buffersize 70 is here {out.precision (6); Out.setf (iOS :: fixed, ios :: floatfield); Out.written ((Const four *) and Buffer [i], Float); }}
This function is called for every ticker of the timer. It is taking so much time, even after the experiment I still wait for the program to receive and save data It has to be done in the file. Due to its slowing cause, it requires opening a binary file for each tick and finding the location where it was previously closed. If there is a method then I can open the file all the time and place location indicator in one place where it was previously closed I could be able to sharpen it. Do any suggestions on any body? Actually how even after going out of functions, how can open and open?
C ++ method in one class, timer function and stream object, something like this:
< Pre> class buffer viver {std :: ofstream out; Public: Clear Buffer Vetter (Const Stroot :: String and Filename): Out (filename.c_str (), std :: ios :: app) {out.precision (6); Out.setf (std :: ios :: fixed, std :: ios :: floatfield); } Zero write file (float * buffer, stade :: size_t buffer size) {for (size_t i = 0; i Just make an example of this, and call your writeToFile ()
function in each timer tick. When the example is destroyed, the file will be closed. You can also do flush ()
at the end of each writeToFile ()
if you want to.
In the case of usage, you can do this for example:
int main () {buffer violet buff ("MyBufferFile.dat") ; Timer T = Make Timer (); T.setTickCallback (buf, and buffer: :: writeToFile); // call buff.writetoothfile () t on each tick Start (); WaitUntilFinished (T); }
Important point buffer viver
exists for example, as long as the entire timer runs the operation. Unless the buffer viverce
example is present, its outside
member will also be present and the related file will remain open.