[Qt-interest] working with qfile
Bob Hood
bhood2 at comcast.net
Mon Sep 26 15:43:45 CEST 2011
On 9/26/2011 7:14 AM, Sahana Bhaskar wrote:
>
>
>
> Hi all,
>
>
>
> I am trying to read a text file using the qfiles. I need to see if the file
> contains a specific string and if it does I need to replace the whole line
> with a new line. I tried using QTextStream. Could anyone please help me out
> with this?
>
Simple enough (not tested, but should give you the idea):
QRegExp text_to_find("^Hello world!$");
QFile out_file(out_file_name);
if(!out_file.open(QIODevice::WriteOnly | QIODevice::Text))
// deal with this error
return;
QFile in_file(in_file_name);
if(!in_file.open(QIODevice::ReadOnly | QIODevice::Text))
// deal with this error
return;
in_text = in_file.readAll();
QTextStream text_out(&out_file);
QList<QByteArray> lines = in_text.split('\n');
foreach(QByteArray line, lines)
{
if(text_to_find.indexIn(line, 0) != -1)
// this line contains the text to be replaced
text_out << "Goodbye world!\n";
else
text_out << line
}
in_file.close();
out_file.close();
Render me gone, |||
Bob ^(===)^
---------------------------------oOO--(_)--OOo---------------------------------
"One of the penalties for refusing to participate in politics is that you
end up being governed by your inferiors." -- /Plato/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110926/a5b6ce0b/attachment.html
More information about the Qt-interest-old
mailing list