【问题标题】:Qt: cannot open file for writingQt:无法打开文件进行写入
【发布时间】:2016-05-25 00:37:21
【问题描述】:

我尝试使用 QFile 类从 Qt-widget 应用程序访问一个简单的文本文件以读取文字。逐行读取文件作为字符串可以正常工作。但是打开它准备写入失败。以下代码检查文件是否存在并尝试设置正确的权限,但最终文件将无法打开。 这是失败的代码:

#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();
}

Qt 返回此消息:

exists?               true
writable?             false
permissions before?   QFlags(0x4|0x40|0x400|0x4000)
permissions set?      false
permissions after?    QFlags(0x4|0x40|0x400|0x4000)
opened?               false
errors?               "Unknown error"
errnum?               5
QIODevice::write (QFile, ":/test.dat"): device not open

如果我将 open-function 中的参数更改为 QIODevice::ReadOnly,则该文件可以毫无问题地读取,但 QIODevice::WriteOnly 会失败。为什么同样的事情对写作也不起作用?是许可吗?为什么我打电话给setPermissions 后权限没有改变?我在 Ubuntu 14.04 上以 root 身份运行 Qt。并且test.dat 拥有用户拥有的全部权利-rwxrwxrwx。 有人可以帮忙吗? 谢谢!

【问题讨论】:

    标签: qt file permissions


    【解决方案1】:

    作者在写入由具有提升权限的控制台进程创建的文件时遇到了与 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" 并回答“是”,然后上面的代码在程序进程的上下文中创建新文件后显示:

    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。

    我猜,它并不能确保您从中运行的程序的权限。更新:确保您以适当的权限运行程序,例如在这种情况下是根。

    【讨论】:

    • 正如我所提到的:这对我不起作用。我尝试了所有可以写入的 OpenModeFlags,但它们都不起作用。
    • 哦,对了,我的忽略。尝试完全限定的操作系统路径。 No ~ and no :/" 后者可能被解释为内部 Qt 资源(只是猜测)。尝试将该文件也放在其他地方。
    • 感谢您的提示。我使用了完全限定的操作系统路径。如果我手动更改权限,则权限在 Qt 应用程序中保持不变,但不幸的是,文件仍然无法打开准备写入。
    • 更新了我的答案。它是为文件设置的进程上下文相关权限。
    • 感谢您的帮助!我创建了一个新项目并从旧项目中复制粘贴代码。我不知道为什么,但现在一切正常。即使我在没有 root 权限的情况下运行 Qt Creator。我唯一改变的是,要写入的文件没有添加到 Qt 项目中。可能存在冲突,因为该文件仍在 Qt Creator 中使用,但也被程序使用。这会发生吗?
    猜你喜欢
    • 2021-03-31
    • 1970-01-01
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-23
    相关资源
    最近更新 更多