Search code examples
c++qtqfile

Problem in creating QFile inside a location other than the current directory


I am using Qt 4.7 installed on Ubuntu 10.04 .... I can't create a qt file inside a location other than the current directory "."

This code works well:

QFile file("data.txt");
if (!file.open(QIODevice::Append))
{
    qDebug("ERROR WHILE OPENING THE FILE TO APPEND Data");
    return ;
}

but this code fails (I think because of the permissions):

QFile file("/var/lib/mysql/testdb/data.txt");
if (!file.open(QIODevice::Append))
{
    qDebug("ERROR WHILE OPENING THE FILE TO APPEND Data");
    return ;
}

So, how can I solve this problem and give the program permissions to create the file wherever at the file system??


Solution

  • You solve this quickly in one of these ways:

    • Run as root (this is a big no-no)
    • As root, change the permissions of the directory you need, chmod /the/dir o+rwx - this lets everyone write to the directory.
    • As root, change the owner of the directory you need, chown myuser /the/dir - this lets your user write to the directory.

    You solve this correctly by sticking to directories that your current user can access - permissions are there for a reason.