奇怪的是,我通过add Existing Files将所需的文件添加到资源中,文件就在那里。我运行qmake ("Build->Run qmake")使文件可用。第一个问题:我不能从输出终端!向文件中写入任何内容,但是当我手动写入文件时,每次运行输出终端时,输出终端都会显示更改。第二个问题:仍然显示QIODevice::read: device not open!下面是我的代码:
#include <QCoreApplication>
#include <QDebug>
#include <QFile>
#include <QString>
#include <QTextStream>
#include <iostream>
void wFile(QString Filename)
{
QFile nFile(Filename);
QTextStream str(&nFile);
qDebug() << "what do you want to write in the desired file: ";
str.readLine();
if (!nFile.open(QFile::WriteOnly | QFile::Text))
{
qDebug() << "could not open the file";
return;
}
nFile.flush();
nFile.close();
}
void read (QString Filename){
QFile nFile(Filename);
if(!nFile.open(QFile::ReadOnly | QFile::Text))
{
qDebug() << "could not open file for reading";
return;
}
QTextStream in(&nFile);
QString nText = in.readAll();
qDebug() << nText;
nFile.close();
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString nFilename =":/MyFiles/DocumentArminV.txt";
wFile(nFilename);
read(nFilename);
return a.exec();
}下面是代码的输出终端:

发布于 2018-09-03 19:06:16
保存在qresource中的文件是只读,因为它们是可执行文件的一部分,所以您不能写入或修改它们。
docs:

目前,Qt总是将数据直接存储在可执行文件中,甚至在Windows、macOS和iOS上也是如此,在这些平台上,操作系统为资源提供了本机支持。..。
https://stackoverflow.com/questions/52148390
复制相似问题