Hello,
I'm a newbie on programming QT. I have a problem, please look at the details;
I want to search in a file and if there exists such a text "foo bar" then i'll break operation, if it does not exists i'll append it to the file. Here's my code:
QFile file("c:\\abc.cfg");
if(!file.exists())
{
return false;
}
{
return false;
}
QString configFileContent
= file.
readAll();
if (!configFileContent.contains("foo bar"))
{
out << "foo bar\n";
}
file.close();
QFile file("c:\\abc.cfg");
if(!file.exists())
{
return false;
}
if (!file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Append))
{
return false;
}
QString configFileContent = file.readAll();
if (!configFileContent.contains("foo bar"))
{
QTextStream out(&file);
out << "foo bar\n";
}
file.close();
To copy to clipboard, switch view to plain text mode
The problem is, it opens file but i cannot get the content of the file. After that it thinks foo bar doesnt exists so it writes at the end of the file. But foo bar is there.
Why did it cannot get the content of the file and search foo bar? Where's the problem? How can i solve it?
Your regards,
PS: Also i tried this code too, but i have the same problem..
QFile file("c:\\abc.cfg");
if(!file.exists())
{
return false;
}
{
return false;
}
QString configFileContent
= in.
readAll();
if (!configFileContent.contains("foo bar"))
{
out << "foo bar\n";
}
file.close();
QFile file("c:\\abc.cfg");
if(!file.exists())
{
return false;
}
if (!file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Append))
{
return false;
}
QTextStream in(&file);
QString configFileContent = in.readAll();
if (!configFileContent.contains("foo bar"))
{
QTextStream out(&file);
out << "foo bar\n";
}
file.close();
To copy to clipboard, switch view to plain text mode
EDIT: Corrected missing part (in changed to file). Sorry about the mistake
Bookmarks