PDA

View Full Version : Where's my wrong?(Related to QFile & QTextStream)



musaulker
27th March 2007, 09:31
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;
}
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();


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;
}

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();

EDIT: Corrected missing part (in changed to file). Sorry about the mistake

guilugi
27th March 2007, 09:37
I think there's a line of code missing...
Your "in" object is a QTextStream, right ?

You have to link it to your file like this

QTextStream in(&file);

This must be done after your "file.open()" test.

EDIT :
OK, you corrected...
You don't need to use a QTextStream by the way, you can do this directly :

QString configFileContent = file.readAll();

Guilugi.

musaulker
27th March 2007, 09:47
I've tried both methods (QFile's readAll() and QTextStream's readAll() ) but i cannot get the content of the files. But I know that there is some lines in the file.. Cannot find the mistake :(

guilugi
27th March 2007, 09:50
If you output the result of file.size(), is it null ?
Maybe Qt can't open it (bad permissions, wrong path...) ?

wysota
27th March 2007, 10:03
Try opening without the append mode.

musaulker
27th March 2007, 10:12
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)


qint64 fileSize = file.size(); // 3651
QString configFileContent = file.readAll(); // 0
int configFileSize = configFileContent.count(); // 0


What do you think now?

musaulker
27th March 2007, 10:18
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..

guilugi
27th March 2007, 10:30
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 :)

musaulker
27th March 2007, 10:35
Oh :) I get the point

Thanks again;)