C ++ text file contents are deleted after power loss

I write some data to a .txt file at runtime. The problem is that sometimes I need to turn off the power to the system, but I would still like to save the recorded data.

Here is the code that I use to write to a txt file:

std::ofstream file; // placed inside the header file file.open("filename.txt"); // placed in the class constructor file << "data " << std::endl; // repeatedly called inside a loop 

At run time, the data is cleared, as I open the file while the program is running, and I see updates. But if I turn off the power, the result will be an empty file or just one line written on it. I also tried manually resetting (), but that didn't work either. OS - Linux Yocto Poky.

+5
source share

All Articles