PDA

View Full Version : New line when writing a File



locke
22nd February 2010, 19:40
Hi guys:

I got a problem that seems to be very easy to solve. I want to write a file (previously selected by the user) with some values in columns, so it is necessary to insert a "new line" character "\n"

I've tried but it doesn't work. In contrast, "\t" for a tab works properly.

I post the code here:

QFileDialog dialog(this);
QString output = dialog.getSaveFileName( this,"Save file export", "/tmp/test.txt", "Text (*.txt)");

QFile outputFile(output);
outputFile.open(QFile::ReadWrite);

QTextStream out(&outputFile);

for(int i = 0; i<myVector.size(); i++) //My vector is a QVector<double>
out << myVector.at(i) << "\n" <<endl;

The value is properly saved, but not the new line, so all values are together at the same line.

(I've tried to delete the endl, create a previos QString and then add it, etc,....)

Thanks!!!!!

boudie
22nd February 2010, 21:56
Try this:
out << myVector.at(i) << '\n'; // note: single quotes

Or this:
out << myVector.at(i) <<endl; // should also work

locke
23rd February 2010, 08:20
I've also tried it, but it doesn't work. It is a very strange error, but seems to have a quite simple solution that I can not find

spirit
23rd February 2010, 08:38
if you write data in text format you should open a file in text mode


outputFile.open(QIODevice::ReadWrite | QIODevice::Text);

endl should work.

locke
23rd February 2010, 08:50
Thank you so much spirit. It works now!!!!!

Thanks!!!

prabhakaran
17th May 2011, 11:27
Thank you spirit. It helped me also.