PDA

View Full Version : Filing Problem



usman
13th July 2009, 08:31
Hi every one,

I started using QT recently, i have a text file which contain thousand lines and i want to change the particular lines of the files at particular location. currently i am doing it by buffering the whole file which is just a mess for me. Is there is anyway to change the particular lines of the text file. i am taking the input through the text boxes and want to change those value in the text file.

Thanks

Usman

wysota
13th July 2009, 08:47
If the length of the text you want to replace is constant then yes, you can overwrite only the needed characters and it will work. If the text can be of arbitrary length, you have to rewrite the whole file.

usman
13th July 2009, 17:55
what you mean by the fix length if you mean how many characters you have to change than yes i know how many characters to change can you quote the method of changing the text

thanks

wysota
13th July 2009, 18:33
Open the file in read/write mode, seek to the position you want to overwrite and write new data. It will replace the old one.

usman
14th July 2009, 04:44
hi
Can you give me some code example please.

nish
14th July 2009, 06:18
if your file reads like this


Line no 1
Line 2 is bigger than 1
Line 3 is small.

then you have no choice but to write the whole file again.

but if all of your lines are of same length then you can use
QTextStream::seek();

yogeshgokul
14th July 2009, 08:17
What about jumping on a line just by searching \n or a combination of \n\t x-1 times. If desired line number is x. ;)

wysota
14th July 2009, 08:30
hi
Can you give me some code example please.

Come on, man. We're not doing your school projects for you here. We can help but in the end you have to show a bit of initiative yourself. You have been given everything you need - just go and read QFile docs.