PDA

View Full Version : Problem in writing to a file



darlene
30th April 2011, 04:35
Hello! i am new to Qt and i have a problem in writing to a file.

when i specify ReadOnly, it seems to be fine. it can open and read the contents of the file. but if i write WriteOnly or ReadWrite, it cannot even open the file. Why is that so?

hope you can help me on this. BTW, i got the code from the Qt documentation.

Thanks.

Lykurg
30th April 2011, 10:28
Obviously you don't have the write permission for the file.

squidge
30th April 2011, 11:48
Or perhaps even the directory. If you are writing on Windows 7 for example, directories such as Program Files are read only unless you request permission from the user via UAC.

darlene
1st May 2011, 06:10
Thanks for your reply.

it is just a text file i added as a resource file. Can we not write on resource files?

my code looks like this



QFile file(":/files/fav.txt");
if (!file.open(QIODevice::ReadWrite | QIODevice::Text)){
qDebug("file not opened");
qDebug(file.errorString().toStdString().c_str());
return;
}


and the output of the errorString is "Unknown error".

squidge
1st May 2011, 10:09
Resources are part of your applications executable. If you were to write to them it would destroy the applications integrity, therefore writing is not allowed.

darlene
17th May 2011, 07:17
hello. thank you very much..

if that is the case, how do i save a file (like a .json file) that will be downloaded from the internet to be used by my mobile app?

where are those files saved?

squidge
17th May 2011, 07:41
That depends on whether you need to store it after your application has exited or not. Most mobile apps have access to at least some NV storage.

darlene
17th May 2011, 23:07
I see.

Yes, the file is going to be used by the app so it needs to be saved. The problem is i have no idea where should it be saved.

i really have no idea on this. Thank you very much for the reply.

squidge
17th May 2011, 23:17
You shouldn't need to specify a location as the default is typically your private storage area.

DanH
18th May 2011, 04:42
http://doc.qt.nokia.com/4.7/qdir.html#homePath