Home > Software design >  Qt cannot write to file. File exists, isn't open, "Unknown error" (Windows OS)
Qt cannot write to file. File exists, isn't open, "Unknown error" (Windows OS)

Time:06-29

Would really appreciate your help. I'm new to Qt and C .

I managed to get this working previously, but for some reason am no longer able to do so. I haven't touched anything to do with the writing of files, but all functions pertaining to file writing no longer seem to function. Here's an example of one of the simpler write functions:

#include <QStringList>
#include <QDir>
#include <QFile>
#include <QString>
#include <QTextStream>


void HandleCSV::writeToPIDCSV(UserAccount newUser)
{
    // Storing in userPID db
    QString filePath = returnCSVFilePath("dbPID");
    qDebug() << "File path passsed to writeToPIDCSV is " <<  filePath;

    // Open CSV filepath retrieved from associated dbName
    QFile file(filePath);
    if (!file.open(QIODevice::ReadWrite | QIODevice::Append))
    {

        qDebug() << file.isOpen() << "error " << file.errorString();
        qDebug() << "File exists? " << file.exists();
        qDebug() << "Error message: " << file.error();
        qDebug() << "Permissions err: " << file.PermissionsError;
        qDebug() << "Read error: " << file.ReadError;
        qDebug() << "Permissions before: " << file.permissions();
        // I tried setting permissions in case that was the issue, but there was no change
        file.setPermissions(QFile::WriteOther);
        qDebug() << "Permissions after: " << file.permissions();


    }
//    if (file.open(QIODevice::ReadWrite | QIODevice::Append))

    else
    {
        qDebug() << "Is the file open?" << file.isOpen();

        // Streaming info back into db
        QTextStream stream(&file);
        stream << newUser.getUID() << "," << newUser.getEmail() << "," << newUser.getPassword() << "\n";

    }
    file.close();
}

This gets me the following output when run:

File path passsed to writeToPIDCSV is  ":/database/dummyPID.csv"
false error  "Unknown error"
File exists?  true
Error message:  5
Permissions err:  13
Read error: 1
Permissions before:  QFlags(0x4|0x40|0x400|0x4000)
Permissions after:  QFlags(0x4|0x40|0x400|0x4000)

The file clearly exists and is recognised when run, but for some reason file.isOpen() is false, and the permissions show that the user only has read (not write) permissions that are unaffected by permission settings.

Would someone have a clue as to why this is happening?

Many thanks in advance

Update

Thanks @chehrlic and @drescherjm - I didn't realise that I can only read but not write to my resource file!

Using QStandardPaths allows me to write to my AppDataLocation. I've included the code below for others who might encounter similar issues, adapted from https://stackoverflow.com/a/32535544/9312019

#include <QStringList>
#include <QDir>
#include <QFile>
#include <QString>
#include <QTextStream>
#include <QStandardPaths>

void HandleCSV::writeToPIDCSV(UserAccount newUser)
{
    auto path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
    if (path.isEmpty()) qFatal("Cannot determine settings storage location");
    QDir d{path};
    QString filepath = returnCSVFilePath("dbPID");

    if (d.mkpath(d.absolutePath()) && QDir::setCurrent(d.absolutePath()))
    {
        qDebug() << "settings in" << QDir::currentPath();
        QFile f{filepath};
        if (f.open(QIODevice::ReadWrite | QIODevice::Append))
        {
            QTextStream stream(&f);
            stream << newUser.getUserFirstName() << ","
                   << newUser.getUserIDNumber()  << "\n";
        }
    }
}

Am now testing to make this work with my Read functions.

If there's any way to be able to write to the Qt program's folder (or build folder), I'd really appreciate it!

CodePudding user response:

You can not write to a Qt resource. If you want to update/write to a file you should put the file into a writeable filesystem. To e.g. place it in the appdata folder you can use QStandardPaths::writeableLocation(QStandardPaths::ApplicationsLocation)

  • Related