Qt: cannot open file for writing -
i try access simple text file qt-widget application qfile class reading writing. reading file line line string works fine. opening ready write fails. following code checks if file exists , tries set proper permissions, in end file won't open. here failing piece of code:
#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 append"; file.close(); return app.exec(); }
qt returns message:
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
if change parameter in open-function qiodevice::readonly
file readable without problems, failing qiodevice::writeonly
. why doesn't same thing work writing well? permission? , why don't permissions change after called setpermissions
? run qt root on ubuntu 14.04. , test.dat
has full rights -rwxrwxrwx
owned user. can help? thanks!
the author having linux-related problem writing file created console process elevated privileges. have reproduced problem , when attempted remove file with:
vi \home\myuser\documents\f.txt // create file console rm \home\myuser\documents\f.txt // try remove console
i got "rm: remove write-protected regular file "\home\myuser\documents\f.txt"
, responded "yes" , code above shows after creating new file in context of program's process:
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
i run qt creator root on ubuntu 14.04.
it not ensure privileges of program run it, guess. update: make sure run program appropriate permissions e.g. root in case.
Comments
Post a Comment