Run qmake") 使文件可用。第一个问题:我无法从输出终端向文件写入任何内容!但是当-6ren">
gpt4 book ai didi

c++ - 如何在 Qt 5 中写入和读取 QResource 文件?

转载 作者:太空宇宙 更新时间:2023-11-04 12:38:27 25 4
gpt4 key购买 nike

很奇怪,我通过添加现有文件... 将所需文件添加到资源中,文件就在那里。我运行 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();
}

这是代码的输出终端: out put

最佳答案

保存在qresource中的文件只读,因为它们是可执行文件的一部分,因此您不能编写或修改它们。

docs:

enter image description here

目前,Qt 始终将数据直接存储在可执行文件中,即使在操作系统为资源提供 native 支持的 ​​Windows、macOS 和 iOS 上也是如此。 ...

关于c++ - 如何在 Qt 5 中写入和读取 QResource 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55394848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com