【发布时间】:2017-03-27 10:45:58
【问题描述】:
我正在尝试使用以下代码打开现有文件以在其末尾附加数据:
void AddPharmacyForm::addInsertToFile(QString insert)
{
QFile inserts(":/new/prefix1/insertstatements.txt");
if(!inserts.exists())
qDebug() << "File does not exist";
if(inserts.isOpen())
qDebug() << "file is open";
if(inserts.open(QFile::ReadWrite | QFile::Text))
{
// Another workaround- could not open file with append flag
qDebug() << "im here!";
QString currentInserts;
QTextStream out(&inserts);
out >> currentInserts;
out << endl << insert;
inserts.close();
}
else
{
QMessageBox::information(this, tr("Error"), tr("Cannot add new pharmacy! "
"Please contact program designer."
));
qDebug() << "error code: " + QString::number(inserts.error());
return;
}
}
此代码的输出是带有错误的 QMessageBox,在 qDebug 中它会生成以下行:
"error code: 5"
它不会通知文件不存在和文件正在打开。我还尝试使用不同的标志打开文件:QFile::ReadWrite、QFile::append、QFile::WriteOnly 以及 QIODevice 中的相同模式。错误代码仍然相同。当我从另一个类打开文件时,文件打开时没有错误(这不是访问错误)。
什么可能导致这个问题?
【问题讨论】:
-
使用“:/”的意思是要写一个qrc捆绑文件??我认为这根本不可能。
-
那么qrc中包含的文件是只读的?没有办法在程序上下文中编辑它们?
-
你指的是预编译的资源文件,只能读取。如果要输出,则需要在磁盘上创建本地副本并将内容写入其中。