問題描述
奇怪,我通過添加現有文件...將所需文件添加到資源中,文件就在那里.我運行 qmake ("Build->Run qmake") 以使文件可用.第一個問題:我無法從輸出終端向文件中寫入任何內容! 但是當我手動寫入文件時,每次運行時輸出終端都會顯示變化它.第二個問題:它仍然顯示 QIODevice::read: device not open !這是我的代碼:
#include #include #include #include #include #include void wFile(QString 文件名){QFile nFile(文件名);QTextStream str(&nFile);qDebug() <<"你想在想要的文件中寫什么:";str.readLine();if (!nFile.open(QFile::WriteOnly | QFile::Text)){qDebug() <<無法打開文件";返回;}nFile.flush();nFile.close();}無效讀取(QString文件名){QFile nFile(文件名);if(!nFile.open(QFile::ReadOnly | QFile::Text)){qDebug() <<無法打開文件進行閱讀";返回;}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(n文件名);讀取(n文件名);返回 a.exec();}
這是代碼的輸出終端:
保存在
目前,Qt 始終將數據直接存儲在可執行文件中,即使在操作系統為資源提供本機支持的 ??Windows、macOS 和 iOS 上也是如此....
It's strange, I add desired file into the resources via Add Existing Files..., the file is there. I run qmake ("Build->Run qmake") to make the file available. The first issue: I can't write anything into the file from output terminal! But when I manually write into the file, the output terminal shows the change every time I run it. Second issue: it still says QIODevice::read: device not open ! Here's my code:
#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();
}
And here's output terminal of the code:
The files saved in a qresource are read-only since they are part of the executable so you can not write or modify them.
docs:
Currently, Qt always stores the data directly in the executable, even on Windows, macOS, and iOS, where the operating system provides native support for resources. ...
這篇關于如何在 Qt 5 中寫入和讀取 QResource 文件?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!