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

时间:2016-05-25 00:37:22

标签: qt file permissions

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

/* add y_: to the drop if you don't need them */
data welfare_periods (drop=i); 
  format ID start end duration censor 8.;
  set welfare;
  array y(*) y_:;    
  censor = 0;
  start = .;
  end = .;
  duration = .;

  /* Loop over every column y_... */
  do i = 1 to (dim(y)-1);
    if y(i) = "welfare" then do;
      /* Check if we have a new start */
      if duration = . then do;
        start = i;
        end = i; /* We'll increment this later if need be. */
        duration = 1; /* We'll increment this later if need be. */
      end;
      else if y(i-1) = "welfare" then do;
        end = end + 1;
        duration = duration + 1;
      end;
    end;

    /* When y(i) = "other" */
    else if y(i) = "other" then do;
      if duration > 0 then do;
        /* Output the row and reset the start/end/duration variables */
        output;
        start = .;
        end = .;
        duration = .;
      end;
    end;

    /* Decide what to do when we reach last element minus 1 */
    /* We know here that y(i) = "welfare", since the last */
    /* "else if" block took care of y(i)'s equal to "other" */
    if i = dim(y) - 1 and duration > 0 then do;
      if y(i+1) = "welfare" then do;
        end = end + 1;
        duration = duration + 1;
        censor = 1;
        output;
      end;
      else if y(i+1) = "other" then output;
    end;
  end;
run;

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-function中的参数更改为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 ,文件可以正常读取,则QIODevice::ReadOnly失败。为什么写作也不一样?这是许可吗?为什么在我调用QIODevice::WriteOnly后权限没有改变?我在Ubuntu 14.04上以root身份运行Qt。 setPermissions拥有用户拥有的完整权限test.dat。 有人可以帮忙吗? 谢谢!

1 个答案:

答案 0 :(得分:2)

作者遇到与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。

我猜,它无法确保您从中运行的程序的权限。更新:确保您使用适当的权限运行程序,例如在这种情况下扎根。