PDA

View Full Version : Problem: Reading and editing text file data



dipeshtech
2nd May 2011, 19:28
I have a text file having data something like:



ww1
ww2
ww3
ww4


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.

squidge
2nd May 2011, 20:48
Rewrite the entire file:

(Psuedocode, you would use QFile for this)


while (!eof(file))
{
str = readlinefromfile();
if (str == "ww3") writelinetofile("$$ww3"); else writelinetofile(str);
}

dipeshtech
2nd May 2011, 23:47
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...



Input file:

bb1
bb2
bb3


o/p getting:

bb1
bb2
bb3
bb1
$$bb2
bb3

desired o/p:
only:

bb1
$$bb2
bb3








QFile file_bl1("E:\\SMS\\dSenderBlackList.txt");
file_bl1.open(QIODevice::ReadWrite | QIODevice::Text);
file_bl1.reset();
QTextStream bl_in1(&file_bl1);
QString bl_line1;
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.