Qt,二进制文件读写建议

二进制文件写出

cpp 复制代码
// fileName含后缀, 生成随机临时文件名(Tmp_时间戳_进程ID_三位随机数_正式文件名)
QString AIbinIOcontrol::geneRandomTmpFilename(QString fileName)
{
    // #include <QDataStream>
    // #include <QRandomGenerator>
    // #include <QDateTime>
    // #include <QCoreApplication>
    const QString timeStamp = QDateTime::currentDateTime().toString("yyyyMMddhhmmsszzz");
    const qint64 pid = QCoreApplication::applicationPid();
    const int randNum = QRandomGenerator::global()->bounded(1000);
    return (QString("Tmp_%1_%2_%3_")+fileName).arg(timeStamp).arg(pid).arg(randNum);
}
bool WriteOutAIres(QString folderPath, QString fileName, string VersionN)
{
    // ------[保存临时文件]------ //
    BT_OCT::creatSavePath(folderPath);  // 创建保存文件的文件夹
    const QString TmpFileName = geneRandomTmpFilename(fileName);  // 创建临时文件名
    QString filePath = folderPath + "/" + TmpFileName;
    QFile file(filePath);
    if (!file.open(QIODevice::WriteOnly)) {
        cout << "结果保存失败,因为文件地址异常:"+filePath.toStdString() << endl;
        return false;
    }
    QDataStream out(&file);
    out << VersionN;                        // 写入版本号名称
    out << 各种数据(int,double,map,vector);  // 写入数据信息
    out << 各种数据(int,double,map,vector);  // 写入数据信息
    file.close();
    // ------[将保存的临时文件rename为正式文件名]------ //
    QString realfilePath = folderPath + "/" + fileName;
    QFile realfile(realfilePath);
    if (realfile.exists()){
        if (!realfile.remove())
        {
            file.remove();
            cout << "结果保存失败,旧文件无法被删除(临时文件已清理)" << endl;
            return false;
        }
        cout << "旧文件已成功删除" << endl;
    }
    if (!QFile::rename(filePath, realfilePath))
    {
        file.remove();
        cout << "结果保存失败,临时文件重命名成正式保存文件时失败(临时文件已清理)" << endl;
        return false;
    }
    cout << "###################" << endl;
    cout << ">>> 结果成功保存 <<<" << endl;
    cout << "###################" << endl;
    return true;
}

二进制文件读入

cpp 复制代码
bool ReadInAIres(QString folderPath, QString fileName, string VersionN)
{
    BT_OCT::creatSavePath(folderPath);
    QString filePath = folderPath + "/" + fileName;
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly)) {
        cout << "结果读取失败,因为地址异常:"+filePath.toStdString() << endl;
        return false;
    }
    QDataStream in(&file);
    // ------[读出文件名及版本号]------ //
    std::string versionName;
    in >> versionName;
    if (versionName!=VersionN){
        cout << "结果读取失败,因为版本不一致,当前:"+versionName+",期望:"+VersionN << endl;
        return false;
    }
    in >> 数据容器;
    in >> 数据容器
    file.close();
    return true;

}
相关推荐
3095566669 小时前
股票接口重试怎么分类:正常、空数组与错误响应的幂等处理
java·大数据·开发语言·数据库·php·api接口·股票数据api接口
郝学胜-神的一滴10 小时前
Effective STL 条款09:慎重选择删除元素的方法
开发语言·网络·数据结构·c++·算法·stl
geovindu14 小时前
python:Logistic Regression
开发语言·后端·python·算法
摇滚侠15 小时前
《On Java 中文版 基础卷》阅读笔记 内部类 21
java·开发语言·笔记
番茄味城市甜菜15 小时前
二叉树刷题(C语言)
c语言·开发语言
布莱克60515 小时前
Swift 自动引用计数(ARC)详解:原理、循环引用与最佳实践
开发语言·ios·swift·arc·自动引用计数
FungLeo15 小时前
FengCMS·PHP+MySQL 升级实录 ·本地容器环境设计:只读挂载、LOCAL_UID、数据卷权限
开发语言·mysql·docker·php·fengcms
geovindu16 小时前
python: Random Forest
开发语言·后端·python·算法·随机森林
真上帝的左手16 小时前
2. 编程语言-Python-Anaconda
开发语言·python