PDA

View Full Version : Writing to a txt file



nagabathula
7th May 2011, 08:42
Hello every one i am working on a software. where i have to write some data i have in two QString containers into a text file, i am able to do that but i am having problem with the formatting in the text file. I have some channel names in one container and data in other. i need to write the channel name and the data related to it next to each other some thing like.


TE6162 27.441999
TE6163 232.520996
TE6164 26.702999
TE6165 26.139000
TE6166 26.332001
TE6171 27.195999
TE6172 27.190001

this is the code which i have tried but it does't write the data in the format i wanted like above.




file4 = new QFile(newfilenames);
file4->open(QFile::ReadWrite | QFile::Text);
for(int d = 0;d < tempidnames.count();d++)
{
out4 << idnames.at(d); //<<data.at(d);
out4 <<'\n';
}
can some one pls tell me how i should go about doing this. I get the channel names properly but when the data is not displayed i did this but the program break when i try to write the data also like this

for(int d = 0;d < tempidnames.count();d++)
{
out4 << idnames.at(d)<<data.at(d);
out4 <<'\n';
}

thank you

stampede
7th May 2011, 08:55
but the program break
Probably data contains less elements than temppidnames.count(), so the data.at() method is crashing.
Btw. is the out4 a valid QTextStream ?
Add some debug statements:


qDebug() << tempidnames.count() << idnames.count() << data.count();
for(int d = 0;d < tempidnames.count();d++)
{
qDebug() << "processing element" << d;
// you can separate the columns using tab character:
out4 << idnames.at(d)<< "\t"<< data.at(d) << "\n";
}

nagabathula
7th May 2011, 11:59
Hello thank you for the help.. That solved my problem. Actually the size was different as you told that is why it was breaking. i am doing this so i did not have any problem with out4.

QTextStream out4(file4);

thank you :)