C++ 如何在Qt5中写入和读取QResource文件?

C++ 如何在Qt5中写入和读取QResource文件?,c++,qt,qt5,qfile,qresource,C++,Qt,Qt5,Qfile,Qresource,奇怪的是,我通过添加现有文件将所需文件添加到资源中…,文件就在那里。我运行qmake(“构建->运行qmake”)以使文件可用。 第一个问题:我无法从输出终端将任何内容写入文件但当我手动写入文件时,每次运行时,输出终端都会显示更改。第二个问题:它仍然说:设备未打开! 这是我的密码: #include <QCoreApplication> #include <QDebug> #include <QFile> #include <QString> #i

奇怪的是,我通过添加现有文件将所需文件添加到资源中…,文件就在那里。我运行qmake(“构建->运行qmake”)以使文件可用。 第一个问题:我无法从输出终端将任何内容写入文件但当我手动写入文件时,每次运行时,输出终端都会显示更改。第二个问题:它仍然说:设备未打开! 这是我的密码:

#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();
}
#包括
#包括
#包括
#包括
#包括
#包括
void wFile(QString文件名)
{ 
QFile文件(文件名);
QTextStream str(&nFile);
qDebug()保存在中的文件是只读的,因为它们是可执行文件的一部分,所以您不能写入或修改它们


目前,Qt始终将数据直接存储在可执行文件中,即使在Windows、macOS和iOS上,操作系统在这些操作系统上提供对资源的本机支持。…

保存在qresource中的项目是只读的。@eyllanesc:即使它很短,此注释也应该是一个答案。因为,正如您所述,Resource文件无法写入亲爱的@eyllanesc非常感谢您的回答:)我用你的好答案将这个问题标记为已回答。虽然我不想在一个问题中有两个问题,但如果你发现这个问题的答案也是直截了当的,请给我一个提示。实际上,我的主要目的是如何编写代码来提示用户,并将该用户键入的所需文本写入specific文件,然后读取它并在输出终端中显示。我搜索了它,但还没有完全找到它。:(@Armin change
“:/MyFiles/documentminv.txt”
“documentminv.txt”