首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Qt不能写入文件。文件存在,未打开,“未知错误”(Windows )

Qt不能写入文件。文件存在,未打开,“未知错误”(Windows )
EN

Stack Overflow用户
提问于 2022-06-21 13:58:30
回答 1查看 124关注 0票数 0

会很感激你的帮助。我对Qt和C++很陌生。

我以前成功地做到了这一点,但由于某种原因,我再也不能这样做了。我没有触及任何与文件编写有关的内容,但是与文件写入有关的所有函数似乎都不再起作用了。下面是一个简单的写函数的例子:

代码语言:javascript
运行
复制
#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();
}

这将在运行时获得以下输出:

代码语言:javascript
运行
复制
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)

该文件显然存在并在运行时被识别,但出于某种原因,file.isOpen()是假的,并且权限显示用户只具有不受权限设置影响的读(而不是写)权限。

有人能知道为什么会发生这种事吗?

事先非常感谢

更新

谢谢@chehrlic和@drescherjm我没有意识到我只能读,但不能写到我的资源文件!

使用QStandardPaths允许我向AppDataLocation写入。我已经为可能遇到类似问题的其他人提供了下面的代码,这些代码来自于https://stackoverflow.com/a/32535544/9312019

代码语言:javascript
运行
复制
#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";
        }
    }
}

我现在正在测试如何用我的读取功能来完成这个任务。

如果有任何方法可以写到Qt程序的文件夹(或构建文件夹),我真的很感激!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-21 15:04:49

您不能写入Qt资源。如果要更新/写入文件,则应将该文件放入可写文件系统。例如,将其放在appdata文件夹中,您可以使用QStandardPaths::writeableLocation(QStandardPaths::ApplicationsLocation)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72702138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档