Find and replace
-
@
QString filename = "xxx.txt";
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
return;
}
QTextStream in(&file);
QStringList line;
while (!in.atEnd())
{
line.append(in.readLine());
}
QString lineedit =line.at(1);
qDebug()<<lineedit;
lineedit.replace("Vidhya","Nithya");
qDebug()<<lineedit;file.close(); @
Here the text is changed but it is not reflected in the file .
how will i achive this. -
What do you think does this do ?
@
file.open(QIODevice::ReadOnly | QIODevice::Text)
@ -
i gave QIODevice::ReadWrite also but its not working
-
This part is replacing something in a QString variable (QString lineedit) and not in your QFile or QTextStream.
@
lineedit.replace("Vidhya","Nithya");
@
and you do not write the changes you did to this lineedit variable to your file or textstream anywhere.Also before posting a question like this, that is very likely to have been asked before, try "searching the forum":http://qt-project.org/search?search=replace+text+in+file for "existing threads":http://qt-project.org/forums/viewthread/6203 on this issue.
-
Hi!
As KA510 says, you're opening the file as ReadOnly.
Besides, your line
@QTextStream in(&file);@
could be better understood as
@QTextStream reader(&file);@
AFAIK (which is not much) what you need is something that 'writes' to that or another file:
@QTextStream writer(&anotherFile);
//...
QString lineedit =line.at(1);
lineedit.replace("Vidhya","Nithya");
writer << lineedit;
@I hope it (somewhat) helps!
David Gil
-
Thank for ur reply i achieved it by writing the result to the another file thank for ur help