Results 1 to 8 of 8

Thread: QTextStream : Remove a Line?

  1. #1
    Join Date
    Jan 2007
    Posts
    12
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default QTextStream : Remove a Line?

    QTextStream provides a method to read a line (ASCII) which is QTextStream::readLine, but is there a way I can remove a specific line?

    For instance, I have a text line with below values:

    Qt Code:
    1. This is line 1
    2. This is line 2
    3. This is line 3
    To copy to clipboard, switch view to plain text mode 


    Is there anyway I can use QTextStream to delete line 2 ("This is line 2"), and well, maybe replace it with something else, which should result in:

    Qt Code:
    1. This is line 1
    2. I replaced something here
    3. This is line 3
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextStream : Remove a Line?

    If you want to change the contents of a file (if it's a file), you need to rewrite the rest. There is no way of adding or deleting something in the middle of the file as it is a stream. So the basic algorithm is to read all before the text you want to discard, write it to a file (or just seek after the block) and then read the rest of the file (after the text you want to discard) and write it right after the first block.

  3. #3
    Join Date
    Jan 2011
    Posts
    39
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTextStream : Remove a Line?

    Hi wysota,
    i want to replace a specific line of a file(suppose only first line).One thing,the new line may be longer but Remaining lines(2nd,3rd etc) will be unchanged.i am using qlineedit for read and write data.If you solved this can you please give the code.

    Advance Thanks.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextStream : Remove a Line?

    There is no "solved" here. If you want to change the length of a file by adding or removing something in any place other than the very end of the file, you have to rewrite the file starting with the place where the change occurs and ending at EOF. You can read the whole file into a string, modify the string, truncate the file and write the string back to it but be aware of all the downsides of reading a file of unknown size to a string.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2011
    Posts
    39
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTextStream : Remove a Line?

    thanx wysota for your reply. can you give code.

    Advance Thanks

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextStream : Remove a Line?

    What code???
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jan 2011
    Posts
    39
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTextStream : Remove a Line?

    sample code
    Last edited by masuk; 31st January 2011 at 16:20.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextStream : Remove a Line?

    Qt Code:
    1. int x = 7;
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Find and line endings
    By mikeh in forum Qt Programming
    Replies: 1
    Last Post: 22nd October 2006, 10:16
  2. QRegExp Help; remove all html tag
    By patrik08 in forum Qt Programming
    Replies: 7
    Last Post: 27th July 2006, 13:40
  3. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42
  4. QListWidget-problem
    By Sarma in forum Qt Programming
    Replies: 7
    Last Post: 7th April 2006, 18:49
  5. QSettings again ... how to remove array elements
    By Mike in forum Qt Programming
    Replies: 4
    Last Post: 11th January 2006, 08: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.