etugcey etugcey - 1 year ago 77
Linux Question

ARM Embedded Linux (AM335x), Text File Contents Deleted After Power Off

Kernel: 3.12.30-AM335x-PD15.2.1 (by PHYTEC)

My application requires editing a text file on run time and using it contents next time it powers on. So I created a text file in which I write a simple text, "Disable" or "Enable" using the program I have written with QT C++.

What I realized is, after the program writes the simple text, if I use the command "reboot" on bash, and wait for the program to reboot before I power off the the system (by plugging off its cable), "cat TextFile.txt" command yields "Enable" or "Disable", whichever the program has last written correctly.

However if I don't do a reboot and power the system off right away, and then power on again, the text file remains but the contents are deleted, so "cat TextFile.txt" yields nothing.

I tried to do the same manually, using the below methods:

Method 1:

echo Disable > TextFile.txt
.....wait for it to reboot
cat TextFile.txt

The results is "Disable".

Method 2:

echo Disable > TextFile.txt
.. power off by plugging off the cable
.. power on the system
cat TextFile.txt

No resulting text..

I simply don't want to have to reboot the system for the files to be saved. So I would be happy with executing commands within my QT C++ program to save everything without a reboot; but I do not know the operating system very well, therefore I do not know what is it that I should do to be able to do this.

This is my code my by the way:

QFile file(filename);
// Trying to open in WriteOnly and Text mode
if(! |
qDebug() << " Could not open file for writing";

// To write text, we use operator<<(),
// which is overloaded to take
// a QTextStream on the left
// and data types (including QString) on the right

QTextStream out(&file);
out << "Enable";

Answer Source

As your experiement on the shell has shown this is not strictly a c++ or Qt matter, the file is just not written to disk right away.

The system setup is likely using delayed writing to optimize disk access times, i.e. first writing into in-memory buffers and writing to actual disk every once in a while.

You might want to tune that if you have other programs that write files and expect power loss as a realistic scenario.

Now, for the Qt program in question, you could try using QSaveFile instead of QFile, its commit() asks the system to actually sync to disk.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download