Results 1 to 5 of 5

Thread: update .txt file

  1. #1
    Join Date
    Oct 2007
    Posts
    201
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    19
    Thanked 1 Time in 1 Post

    Default update .txt file

    Hi ALL,
    I'm trying to create a file and update some data in the file.

    e.g Data is like

    Helllo
    Hi
    Qt
    Test

    Below is my code. but last line is overwriting. Ineed new text to be insert a new line and append the string. Plz tell me how to correct this code snipset.

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <iostream>
    3. #include <qfile.h>
    4. #include <qtextstream.h>
    5. using namespace std;
    6.  
    7.  
    8. void File(QString myFilename, QString myData)
    9. {
    10. QFile file(myFilename);
    11. if ( file.open( QIODevice::ReadWrite) )
    12. {
    13. QTextStream stream( &file );
    14.  
    15.  
    16. // while (stream.atEnd())
    17. // {
    18. // stream << QChar((int)'\n');
    19. //
    20. // }
    21. stream <<myData << endl;
    22.  
    23. }
    24. else
    25. {
    26. qDebug( "Could not create file %s" );
    27. }
    28. }
    29.  
    30. int main(int argc, char *argv[])
    31. {
    32. QCoreApplication a(argc, argv);
    33.  
    34. QString file("D:\\test.txt");
    35.  
    36. File(file, "1St Line: Hello");
    37. File(file, "2Nd Line : Hi");
    38. File(file, "3rd Line : Qt");
    39. File(file, "4th Line : Test");
    40. return a.exec();
    41. }
    To copy to clipboard, switch view to plain text mode 

    Thank u all,
    Phillip
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 268 Times in 268 Posts
    Wiki edits
    20

    Default Re: update .txt file

    QIODevice::Append

  3. #3
    Join Date
    Oct 2007
    Posts
    201
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    19
    Thanked 1 Time in 1 Post

    Default Re: update .txt file

    Thank u verymuch. But its not appending in new line. can u plz tell me how to append at new line?
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

    Default Re: update .txt file

    Open the text file with wordpad. (i don't know the exact explanation why notepad doesn't show them, but new-lines are there)

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: update .txt file

    If you are on Windows then you may need the QIODevice::Text flag to cause the end-of-line conversion from "\n" to "\r\n".

  6. The following user says thank you to ChrisW67 for this useful post:

    Zlatomir (23rd June 2010)

Similar Threads

  1. To update iTunes Library File .
    By merry in forum Qt Programming
    Replies: 1
    Last Post: 28th April 2010, 12:58
  2. Replies: 5
    Last Post: 30th January 2010, 11:09
  3. update the xml file from QT program
    By rk0747 in forum Newbie
    Replies: 5
    Last Post: 28th January 2010, 19:20
  4. Replies: 1
    Last Post: 11th November 2009, 18:28
  5. How to Update in .ini file?
    By phillip_Qt in forum Qt Programming
    Replies: 1
    Last Post: 15th April 2008, 14:40

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
  •  
Qt is a trademark of The Qt Company.