Problem: Reading and editing text file data
I have a text file having data something like:
I want to search for a particular string in this file, so i am reading this file. When the word is found say "ww3" , i wish to edit that entry to something like $$ww3 or ( add anything). I tried using pos() and seek(), but it's deleting all text data from file leaving one or two chars.
I would like to know some way to do this. Please help.
Thanks in advance.
Re: Problem: Reading and editing text file data
Rewrite the entire file:
(Psuedocode, you would use QFile for this)
Code:
while (!eof(file))
{
str = readlinefromfile();
if (str == "ww3") writelinetofile("$$ww3"); else writelinetofile(str);
}
Re: Problem: Reading and editing text file data
Thanks squidge! But, i am using ReadWrite mode in QFile, which is not rewriting the whole file rather it is writing the desired content at the end of file. Something like this...
Quote:
Input file:
bb1
bb2
bb3
o/p getting:
bb1
bb2
bb3
bb1
$$bb2
bb3
desired o/p:
only:
bb1
$$bb2
bb3
Code:
QFile file_bl1
("E:\\SMS\\dSenderBlackList.txt");
file_bl1.reset();
bl_line1 = bl_in1.readLine();
while(!bl_line1.isNull())
{
if(str==bl_line1)
{
bl_in1<<"$$"<<str<<"\n";
}
else
{
bl_in1<<bl_line1<<"\n";
}
bl_line1 = bl_in1.readLine();
}
I am trying , but it would be good if someone can help me in figuring out how to perform aforementioned. Thanks.
Added after 29 minutes:
Thanks Squidge!! PROBLEM is solved. Just took a different pointer for writing.