Results 1 to 3 of 3

Thread: Writing to a txt file

  1. #1
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Writing to a txt file

    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.
    Qt Code:
    1. TE6162 27.441999
    2. TE6163 232.520996
    3. TE6164 26.702999
    4. TE6165 26.139000
    5. TE6166 26.332001
    6. TE6171 27.195999
    7. TE6172 27.190001
    To copy to clipboard, switch view to plain text mode 
    this is the code which i have tried but it does't write the data in the format i wanted like above.


    Qt Code:
    1. file4 = new QFile(newfilenames);
    2. file4->open(QFile::ReadWrite | QFile::Text);
    3. for(int d = 0;d < tempidnames.count();d++)
    4. {
    5. out4 << idnames.at(d); //<<data.at(d);
    6. out4 <<'\n';
    7. }
    To copy to clipboard, switch view to plain text mode 
    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
    Qt Code:
    1. for(int d = 0;d < tempidnames.count();d++)
    2. {
    3. out4 << idnames.at(d)<<data.at(d);
    4. out4 <<'\n';
    5. }
    To copy to clipboard, switch view to plain text mode 

    thank you

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Writing to a txt file

    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:
    Qt Code:
    1. qDebug() << tempidnames.count() << idnames.count() << data.count();
    2. for(int d = 0;d < tempidnames.count();d++)
    3. {
    4. qDebug() << "processing element" << d;
    5. // you can separate the columns using tab character:
    6. out4 << idnames.at(d)<< "\t"<< data.at(d) << "\n";
    7. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to stampede for this useful post:

    nagabathula (7th May 2011)

  4. #3
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: Writing to a txt file

    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.
    Qt Code:
    1. QTextStream out4(file4);
    To copy to clipboard, switch view to plain text mode 

    thank you

Similar Threads

  1. Problem in writing to a file
    By darlene in forum Newbie
    Replies: 9
    Last Post: 18th May 2011, 04:42
  2. Writing a html file in Qt4
    By AviMittal in forum Qt Programming
    Replies: 5
    Last Post: 23rd July 2009, 11:43
  3. Writing a XML file
    By Djony in forum Qt Programming
    Replies: 7
    Last Post: 5th February 2007, 16:23
  4. XML file writing
    By mbjerkne in forum Qt Programming
    Replies: 2
    Last Post: 24th May 2006, 19:04
  5. Writing an XML file
    By qball2k5 in forum Qt Programming
    Replies: 9
    Last Post: 19th March 2006, 10:58

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.