Results 1 to 4 of 4

Thread: Append file

  1. #1
    Join Date
    Jun 2010
    Posts
    97
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Append file

    Hi,

    I have a file called database.h having following content.

    Qt Code:
    1. [I]===========================================================
    2. #ifndef DATABASE_H
    3. #define DATABASE_H
    4.  
    5. #include <QtCore>
    6. #include <QtSql>
    7. class database
    8. {
    9. public:
    10. database();
    11. bool connectDB();
    12.  
    13. private:
    14. };
    15.  
    16. #endif // DATABASE_H
    17. ========================================================[/I]
    To copy to clipboard, switch view to plain text mode 

    Now I want to append some text in this file after finding some phrase in this.

    e.g. after all #include lines I want to add #include <QtGui>
    or
    after private: string I want to add QSqlQuery qry;

    How to achieve this using seek() function?

    Thanks in advance

    Manish
    Last edited by high_flyer; 10th February 2011 at 09:27. Reason: code tags

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

    Default Re: Append file

    seek() alone will not do, since if you just append stuff to a certain position, it will overwrite other data in that position.
    If you want to insert new content somewhere in the middle, you will have to do the following:
    - Read the whole file out in to a String.
    - Search the position desired (I would use QRegExp).
    - Edit the string
    - Write the edited string to the file - overtiring the file with the new updated data.
    ==========================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
    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: Append file

    You can try to read the file line-by-line ( QTextStream::readLine() ) and use QString::indexOf() to check if current line is interesting to you.
    'seek()' is rather low-level compared to other convenient string search methods

  4. #4
    Join Date
    Feb 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Append file

    Qt Code:
    1. QTextStream in("database.h", QIODevice::ReadOnly );
    2. QString fileContent = qts.readAll();
    3. qts.close();
    4. //use indexOf, lastIndexOf, insert functions of QString here.
    5. //then
    6. QTextStream out("database.h", QIODevice::WriteOnly );
    7. out << fileContent;
    8. out.close();
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How do I append and int to my QString?
    By seink in forum Newbie
    Replies: 3
    Last Post: 31st May 2015, 22:45
  2. Append to a XML Document
    By Goldmmbr in forum Qt Programming
    Replies: 0
    Last Post: 16th November 2009, 16:03
  3. Cannot append to QFile using QIODevice::Append
    By philwinder in forum Qt Programming
    Replies: 4
    Last Post: 17th November 2008, 10:09
  4. QVector - Append
    By krishbhala in forum Qt Programming
    Replies: 1
    Last Post: 20th December 2007, 11:50
  5. QTextEdit::append help
    By tho97 in forum Qt Programming
    Replies: 1
    Last Post: 29th November 2007, 08:10

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.