PDA

View Full Version : How to replace a particular text in a file located at any place..?



Ketan Shah
23rd May 2011, 10:14
hey can any1 tel me how to replace the text in a file which is located at any place......?
and can can i edit any .ini file....below is my code....


QFile file("E:/qtpro/ABC/abc.ini");
file.open(QIODevice::ReadWrite | QIODevice::Text);
file.reset();

QTextStream fl(&file);

QString str;


while (!file.atEnd())

{

str = fl.readLine();

if (str == ";extension=abc_123.dll")
{
fl<<"extension=abc_123.dll"<<str<<"\n";
}
//else
//{
//fl<<str<<"\n";
//}
str = file.readLine();

}

high_flyer
23rd May 2011, 12:06
If you want to mange settings in an ini file, check out QSettings - saves you the trouble of manually worrying about reading and writing in to a file - you just set the settings by they name.

Ketan Shah
23rd May 2011, 15:12
can u tel me, how can i perform the same task for a text file?? is my code correct for it.?

high_flyer
23rd May 2011, 15:36
you need to read the file until where place is where you want to edit it, read the rest of the file, but save the data in memory, seek() back to the place where you want to edit, append your new string, append the rest of the file which you hold in memory, close the file.