[Solved] reading and writing the same file while replacing a string
-
Hi,
What is wrong here?
I try to open a file, read in and while reading replace a string with another. fileName is a qstring which i got from QFileDialog
finally write out the file with the same name than the loaded.@
QFile PerlFile("/fs1/PerlQt/QtCreator/EngineMountStiffness/test.pl");
PerlFile.open(QIODevice::ReadOnly|QIODevice::Text);
QTextStream in(&PerlFile);
QString line = in.readAll();
QString ListPerlFile = line.replace(QString("PchFile"), fileName);
in << ListPerlFile;
PerlFile.close();
@ -
I don't know if you expect the files content to be changed? If so what do you think does QIODevice::ReadOnly mean. ;-)
BTW you cannot just replace content in a text file, you can only append content. You have to read the file content (as you already do) delete the file and create a new file with the same name and the new content.
-
change that to ReadWrite.
-
Hi,
I have to correct KA51O. You can make change in a file "in place" you have to open it (ReadWrite or WriteOnly) and then seek to the right position and write from that point. Seeking then writing will overwrite the current content. Append will happen only at the end of the file and if the option is given to open().
In your case and if test.pl is not to big i would rather do something like:
@
perlFile.open(QIODevice::ReadWrite|QIODevice::Text);
QByteArray fileContent = perlFile.readAll();
fileContent.replace(QString("PchFile"), fileName);
perlFile.seek(0);
perlFile.write(fileContent);
perlFile.close();
@Warning: code not tested
Answer edited
-
[quote author="SGaist" date="1363864179"]Hi,
I have to correct KA51O. You can make change in a file "in place" you have to open it (ReadWrite or WriteOnly) and then seek to the right position and write from that point. That will either overwrite the current content or append depending on the option given to open().
In your case and if test.pl is not to big i would rather do something like:
@
perlFile.open(QIODevice::ReadWrite|QIODevice::Text);
QByteArray fileContent = perlFile.readAll();
fileContent.replace(QString("PchFile"), fileName);
perlFile.seek(0);
perlFile.write(fileContent);
perlFile.close();
@Warning: code not tested[/quote]
Thanks for the correction. One more question. I thought that with using seek you can only overwrite from that position on and that append is only possible at the end of the file?
-
@KA510, you're right, i wasn't clear. I updated my answer.
-
This is what i wanted to do.
somebody have a nicer one?Thanks to u all
@QFile PerlFile("/na/share/LINUX/public/PuZ_Bench/test.pl");
QFile outPerlFile("/na/share/LINUX/public/PuZ_Bench/out.pl");
PerlFile.open(QIODevice::ReadOnly|QIODevice::Text);
outPerlFile.open(QIODevice::WriteOnly|QIODevice::Text);
QTextStream in(&PerlFile);
QTextStream out(&outPerlFile);
while (!in.atEnd()) {
QString line = in.readLine();
QString outline = line.replace(QString("PchFile"), fileName);
out << outline << "\n";
}PerlFile.close(); outPerlFile.close();
@