Where's my wrong?(Related to QFile & QTextStream)
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:
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();
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..
Code:
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();
EDIT: Corrected missing part (in changed to file). Sorry about the mistake
Re: Where's my wrong?(Related to QFile & QTextStream)
If you output the result of file.size(), is it null ?
Maybe Qt can't open it (bad permissions, wrong path...) ?
Re: Where's my wrong?(Related to QFile & QTextStream)
Try opening without the append mode.
Re: Where's my wrong?(Related to QFile & QTextStream)
Quote:
Originally Posted by
guilugi
If you output the result of file.size(), is it null ?
Maybe Qt can't open it (bad permissions, wrong path...) ?
I logged in as Administrator on Windows XP, and there's no file permission problem. Also if there is permission problem, than i couldnt write on these files. But i can (it doesnt search but it appends. very strange)
Code:
qint64 fileSize = file.size(); // 3651
QString configFileContent
= file.
readAll();
// 0 int configFileSize = configFileContent.count(); // 0
What do you think now?
Re: Where's my wrong?(Related to QFile & QTextStream)
Quote:
Originally Posted by
wysota
Try opening without the append mode.
Yes it opend, searched and writed successfully. I've thought that if I need to append it end of the file, then I must use QIODevice::Append flag..
Thank you very much wysota and guilugi for your answers..
Re: Where's my wrong?(Related to QFile & QTextStream)
Well, I missed the append flag...this flag set the textstream at the end of the file...so when you read, there's nothing more to read :)
Fine if it works :)
Re: Where's my wrong?(Related to QFile & QTextStream)
Oh :) I get the point
Thanks again;)