PDA

View Full Version : Outputting to a .txt file



bbad68
23rd January 2010, 01:32
Hey,

How do I output to a .txt file without getting all of those weird symbols?? The program I'm making will generate a file which is NOT .txt format, but I want it to be ledgible if someone decides to open it in .txt format. Let me show you the code before I explain (it's really messy):


QDataStream out(&file);
out.setVersion(QDataStream::Qt_4_5);
out << "Name\tType\t\ttotalarea\tDimx\t\tDimy\t\tNbcells\t \tNbLayers\tLayer#\tLayer%\tLayerArea"
"\tNb\tMeannucleararea\t\tstdvnucleararea\t\tNb\tMe ancellarea\tstdvcellarea\tNb\tMeanNC\t"
"stdvNC\tNb\tMeanMeanRadius\tstdvMeanRadius\tNb\tMe anMaxRadius\tstdvMaxRadius\tNb\tMeanSpericity"
"\tstdvSphericity\tNbPolygons\tmeanPolyarea\tstdvpo lyarea\tNbdist\tmeandist\tstdvdis\n";
for(int i = 0; i < allData.size(); i++)
out << "0" << "0" << "0.0" << ui.xSpinBox->value() << ui.ySpinBox->value() << ui.doubleSpinBox->value()
<< ui.layerList->count() << i << "0.0" << ui.doubleSpinBox_2->value()
<< "0\t0.0\t0.0\t0\t0.0\t0.0\t0\t0.0\t0.0\t0\t0.0\t0.0 \t0" << ui.doubleSpinBox_3->value()
<< ui.doubleSpinBox_4 << "0\t0.0\t0.0" << ui.doubleSpinBox_5->value() << "0.0\t0.0\t0\t0.0\t0.0\n";

So lines 3-6 will essentially be columns headers in the text file, while lines 7-11 should print out some predetermined values (the zeros), and also some other values that are determined during runtime (ie the spinBox values). Although the zeros do get printed, the spinBox values do not, AND the data is all printed onto one line in the .txt file, making it hard to decipher. What can I do to fix this?

PS Is there also a shortcut to avoid having to type '\t' after every word?

Thanks!

EDIT: I figured out how to make the file the way I want, the only question that I still have is what is the syntax for a new line? '\n' isn't working.

Kronen
23rd January 2010, 04:24
I think you should use QTextStream instead of QDataStream. You can use "out << endl();".
In Windows new lines are written '\r\n' not '\n', but you should use endl() to keep your file portable.