Results 1 to 4 of 4

Thread: writing file with QTextStream

  1. #1
    Join Date
    May 2010
    Posts
    42

    Default writing file with QTextStream

    I' using QTextStream class to write file which has already existed some texts.
    and now,I want to replace the first line only.eg:
    hey! //first line
    wellcome to Qt's world! //second line
    I want using "hello!" to replace "hey!". my code is
    QFile file("/home/drcom.conf");
    file.open(QFile::WriteOnly);
    QTextStream out(&file);
    out<<"hello!"<<endl;
    the result is:
    hello!
    but what I want to achieve is:
    hello!
    wellcome to Qt's world!
    the problem is the second line is cleared.how can I implement it ?if someone can tell me?thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: writing file with QTextStream

    If it is only the first line you want to change, then all you have to do is add QIODevice::Append when you open the file, and your code will work as it is.
    But this is not a very nice way of doing it, and is very unflexible.
    It will not work as soon as the word you want to replace is longer then the word you want to replace it with, and/or if the position of the replaced word is not 0.
    The more general way would be to seek() the wanted location, and replace the string you want to replace, making sure that the current word on that location is fully deleted.
    Getting to the correct location can be done in various ways, which one is best suited for you case depends on your requirements.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    May 2010
    Posts
    42

    Default Re: writing file with QTextStream

    if you can tell me that how to use seek() function in detail? It will be best if you take my code for example.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: writing file with QTextStream

    its all there in the documentation.
    http://doc.trolltech.com/4.6/qtextstream.html#seek
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. QTextStream loses position while reading file
    By bjh in forum Qt Programming
    Replies: 2
    Last Post: 13th February 2008, 16:47
  2. Writing a XML file
    By Djony in forum Qt Programming
    Replies: 7
    Last Post: 5th February 2007, 17:23
  3. Reading File using QFile and QTextStream
    By atm in forum Qt Programming
    Replies: 4
    Last Post: 14th July 2006, 00:12
  4. XML file writing
    By mbjerkne in forum Qt Programming
    Replies: 2
    Last Post: 24th May 2006, 20:04
  5. Writing an XML file
    By qball2k5 in forum Qt Programming
    Replies: 9
    Last Post: 19th March 2006, 11: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.