PDA

View Full Version : QTextStream : Remove a Line?



kaydknight
28th February 2007, 10:56
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:


This is line 1
This is line 2
This is line 3


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:


This is line 1
I replaced something here
This is line 3


Thanks in advance!

wysota
28th February 2007, 11:10
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.

masuk
31st January 2011, 11:00
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.

wysota
31st January 2011, 11:43
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.

masuk
31st January 2011, 13:38
thanx wysota for your reply. can you give code.

Advance Thanks

wysota
31st January 2011, 16:47
What code???

masuk
31st January 2011, 17:04
sample code

wysota
31st January 2011, 19:15
int x = 7;