Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt:无法打开文件进行写入_Qt_File_Permissions - Fatal编程技术网

Qt:无法打开文件进行写入

Qt:无法打开文件进行写入,qt,file,permissions,Qt,File,Permissions,我尝试使用QFile类从Qt小部件应用程序访问一个简单的文本文件,以读取一个写操作。以字符串形式逐行读取文件效果良好。但打开它准备写入失败了。下面的代码检查文件是否存在,并尝试设置适当的权限,但最终文件无法打开。 下面是一段失败的代码: #include "mainwindow.h" #include <QApplication> #include <QFile> #include <QDebug> int main(int argc, char *argv

我尝试使用QFile类从Qt小部件应用程序访问一个简单的文本文件,以读取一个写操作。以字符串形式逐行读取文件效果良好。但打开它准备写入失败了。下面的代码检查文件是否存在,并尝试设置适当的权限,但最终文件无法打开。 下面是一段失败的代码:

#include "mainwindow.h"
#include <QApplication>
#include <QFile>
#include <QDebug>

int main(int argc, char *argv[]){
  QApplication app(argc, argv);
  MainWindow w;
  w.show();

    QFile file(":/test.dat");
    qDebug() << "exists?              " << file.exists();
    qDebug() << "writable?            " << file.isWritable();
    qDebug() << "permissions before?  " << file.permissions();
    qDebug() << "permissions set?     " << file.setPermissions(QFileDevice::WriteOther | QFileDevice::ReadOther);
    qDebug() << "permissions after?   " << file.permissions();

    qDebug() << "opened?              " << file.open(QIODevice::Append);
    qDebug() << "errors?              " << file.errorString();
    qDebug() << "errnum?              " << file.error();
    QTextStream out(&file);
    out << "something to append";
    file.close();

  return app.exec();
}
如果我将open函数中的参数更改为
QIODevice::ReadOnly
,则该文件是可读的,不会出现问题,
QIODevice::WriteOnly
。为什么同样的东西不适合写作呢?是许可证吗?为什么在我调用
setPermissions
后权限不更改?我在Ubuntu 14.04上以root用户身份运行Qt。并且
test.dat
拥有用户拥有的
-rwxrwx
的全部权利。 有人能帮忙吗?
谢谢

作者在使用提升的权限写入控制台进程创建的文件时遇到与Linux相关的问题。我已完全再现了该问题,并在尝试删除该文件时使用了:

vi \home\myuser\Documents\f.txt // create file like that from console
rm \home\myuser\Documents\f.txt // now try to remove it from console
我得到
“rm:remove write protected regular file”\home\myuser\Documents\f.txt“
,并回答“yes”,然后在程序进程的上下文中创建新文件后,上面的代码显示:

opened?               true
exists?               true
writable?             true
permissions before?   QFlags(0x4|0x20|0x40|0x200|0x400|0x2000|0x4000)
permissions set?      true
permissions after?    QFlags(0x4|0x20|0x40|0x200|0x400|0x2000|0x4000)
errors?               "Unknown error"
errnum?               0
我在Ubuntu 14.04上以root用户身份运行Qt Creator


我想,它不能确保您从中运行的程序的权限。更新:确保您以适当的权限运行程序,例如在本例中为Root。

正如我所提到的:这对我不起作用。我尝试了所有启用写入的OpenModelFlags,但没有一个起作用。哦,对了,我的疏忽。尝试完全限定的操作系统路径。否~and no:/“后者可能被解释为内部Qt资源(只是猜测)。试着把那个文件放到别的地方。谢谢你的提示。我使用了完全限定的操作系统路径。如果我手动更改权限,Qt应用程序中的权限将保持这种状态,但不幸的是,文件仍然无法打开准备写入。更新了我的答案。这是为文件设置的与进程上下文相关的权限。感谢您的帮助!我创建了一个新项目,并从旧项目复制粘贴了代码。我不知道为什么,但现在一切都很顺利。即使我在没有root权限的情况下运行Qt Creator。我唯一改变的是,要写入的文件没有添加到Qt项目中。可能有冲突,因为文件仍然在Qt创建者中使用,但程序也使用了该文件。这会发生吗?
opened?               true
exists?               true
writable?             true
permissions before?   QFlags(0x4|0x20|0x40|0x200|0x400|0x2000|0x4000)
permissions set?      true
permissions after?    QFlags(0x4|0x20|0x40|0x200|0x400|0x2000|0x4000)
errors?               "Unknown error"
errnum?               0