c++read-writeqfileresource-filesqtextstream

C++ write to resource file with qfile (QIODevice::write (QFile,"x"): device not open)


i have the following problem, i have an resource file called data.txt and i want to open it with write permission. Im using QFile and QTextStream to work with it. I can only open the File with ReadOnly Acces but not with ReadWrite or WriteOnly acces. Export functions with similar code working fine its only not working on the resource file.

I already tried to change the front slashes to double backslashes, i runned hunderd times the qmake and rebuild, i restartet my computer and resotre the resource file. I already checked alot of entries on Stack but wasnt able to find one that resolves my problem. (Most questions were spelling issues like only one backslash).

QFile file(":/savelocation/data.txt");
if (!file.exists())
{
    qDebug()<<"File not exist";
}

file.open(QIODevice::ReadWrite | QIODevice::Text);

if (file.isOpen())
{
    qDebug()<<"File is open";
    QTextStream out(&file);
    out<< "something" << endl;
}
else
{
    qDebug()<<"File is not open";
}
file.close();
file.open(QIODevice::ReadOnly);
if (file.isOpen())
{
    qDebug()<<"File is open as read only";
}
else
{
    qDebug()<<"File is not open as read only";
}
file.close();

Actual result:

My Application output of the code: File is not open File is open as read only Its only possible to open it as ReadOnly for me.

Before i implemented the if i got the following output:

QIODevice::write (QFile,":/savelocation/data.txt"): device not open

Expected result:

The file would be opened with write access.

Thanks in advance.


Solution

  • As per the documentation, resources are embedded in your binary file and are thus read-only, both conceptually and practically. You will have to save your data to a writable location on the filesystem, for example under QDir::home() or the current working directory QDir::current() .