PDA

View Full Version : QFile write isn't stable



ruben.rodrigues
7th July 2010, 15:00
Hi all!

I have a client-server application and the network settings are loaded from a txt file that can be overwritten by the administrator but I am have some problems writing the textstream to the file.

the txt file looks like this:


192.168.1.220
11001


this is the function to write:


QFile file("/opt/esp/Client/Settings/CL_settings.txt");
if (!file.open(QIODevice::WriteOnly))
return;

QTextStream out(&file);
out << ui.lineEdit_16->text() << "\n" << ui.lineEdit_17->text() << endl;

the open and reading part works perfectly but when I write with that function, sometime it writes the first line and then the second, other times writes a blank line in the middle, then 2 blank lines, and maybe then back to normal...

I tried other ways:


out << ui.lineEdit_16->text() << "\n" << ui.lineEdit_17->text();


out << ui.lineEdit_16->text() << "\n"
out << ui.lineEdit_17->text();


out << ui.lineEdit_16->text();
out << ui.lineEdit_17->text();


out << ui.lineEdit_16->text() + out << ui.lineEdit_17->text();


out << ui.lineEdit_16->text() << "\n"
<< ui.lineEdit_16->text();

they all give me the same problem and my co-worker has a program that does the same thing and it works well for him.

thanks in advance

JD2000
8th July 2010, 13:41
Where is the text in your line edits coming from and how is it being set?

wysota
8th July 2010, 21:14
Maybe more than one instance of the application (or some other application) tries to write to the same file at the same time? Also you don't seem to be truncating the file so you overwrite old contents but if new contents is shorter than the old then the part that is not overwritten will still be in the file (so if your file was 20 bytes long and you open it for writing and write 10 bytes, it will still be 20 bytes long).