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

标签 c++ qt qt5 qfile qresource

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

相关文章:

c++ - 如何在 Windows Mobile 下生成 GUID?

c++ - 生命游戏 - 算法问题 C++

C# C++ 互操作回调

c++ - 关闭QMdiSubWindow时出现错误信息

python - 属性错误 : 'StartQT4' object has no attribute 'accept'

c++ - 如何知道写入文件何时完成?

Qt-Creator:如何设置可以在 QTDesigner 中看到的应用程序范围样式表?

Android Studio IDE 找不到 NDK 头文件

c++ - 将宏参数中的序列转换为单独的宏

qt - 如何在 Qt 中从 QToolBar 自定义 QToolButtons?