当前位置: 首页 > news >正文

Qt文件读写

Qt文件读写(Stream流形式)

文件读写相关类

1. QFile类

QFile主要用于文件的打开、关闭等功能;

[override virtual] bool QFile::open(QIODevice::OpenMode mode);

Reimplements: QIODevice::open(QIODevice::OpenMode mode).
Opens the file using OpenMode mode, returning true if successful; otherwise false.
The mode must be QIODevice::ReadOnly, QIODevice::WriteOnly, or QIODevice::ReadWrite. It may also have additional flags, such as QIODevice::Text and QIODevice::Unbuffered.
重新实现:QIODevice::open(QIODevice::OpenMode模式)。
使用OpenMode模式打开文件,如果成功则返回true,否则错误。
模式必须为QIODevice::ReadOnly、QIODevice::WriteOnly或QIODevice::ReadWrite。它也可能有额外的标志,如QIODevice::Text和QIODevice::Unbuffered。

[virtual] void QIODevice::close();

First emits aboutToClose(), then closes the device and sets its OpenMode to NotOpen. The error string is also reset.
首先发出aboutToClose(),然后关闭设备并将其OpenMode设置为NotOpen。错误字符串也会被重置。

2. QTextStream类

读取一行

bool QTextStream::readLineInto(QString *line, qint64 maxlen = 0);

Reads one line of text from the stream into line. If line is 0, the read line is not stored.
The maximum allowed line length is set to maxlen. If the stream contains lines longer than this, then the lines will be split after maxlen characters and returned in parts.
If maxlen is 0, the lines can be of any length.
The resulting line has no trailing end-of-line characters (“\n” or “\r\n”), so calling QString::trimmed() can be unnecessary.
If line has sufficient capacity for the data that is about to be read, this function may not need to allocate new memory. Because of this, it can be faster than readLine().
Returns false if the stream has read to the end of the file or an error has occurred; otherwise returns true. The contents in line before the call are discarded in any case.
将一行文本从流读入行。如果line为0,则不存储读行。
允许的最大线路长度设置为maxlen。如果流包含超过这个长度的行,那么这些行将在maxlen字符之后分割并以部分形式返回。
如果maxlen为0,则行可以是任意长度。
结果行没有尾随的行尾字符(“\n”或“\r\n”),因此调用QString::trim()可能是不必要的。
如果行有足够的容量来存储即将读取的数据,则该函数可能不需要分配新的内存。正因为如此,它可以比readLine()更快。
如果流已读取到文件的末尾或发生错误,则返回false;否则返回true。在任何情况下,调用之前行的内容都将被丢弃。

写入

QTextStream &QTextStream::operator<<(XXX xxx);

文件读取示例

先写入数据到一个csv文件,再读取csv文件的内容

#include <QCoreApplication>
#include <QFile>
#include <QTextStream>
#include <QDebug>


struct TimeValue
{
    double m_dTime;
    double m_dValue;

    TimeValue(double dTime = 0.0, double dValue = 0.0) : m_dTime(dTime), m_dValue(dValue)
    {
        // ...
    }
};

// 写入函数
void WriteFile()
{
    QFile file(QString(QCoreApplication::applicationDirPath() + "/data.csv"));

    if (!file.open(QIODevice::WriteOnly))
        qDebug() << "Write Open file failed!";

    QTextStream textStream(&file);

    textStream << "time, value\n";

    for (int i = 0; i != 10; ++i)
        textStream << QString::number(i) << "," << QString::number(i * 100) << "\n";

    file.close();
}

// 读取函数
void ReadFile()
{
    QFile file(QString(QCoreApplication::applicationDirPath() + "/data.csv"));

    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        qDebug() << "Read Open file failed!";

    QTextStream textStream(&file);

    QString strLine;
    textStream.readLineInto(&strLine);

    QVector<TimeValue> vecTimeValue;

    // 读取一行的字符串数据
    while (textStream.readLineInto(&strLine))
    {
        TimeValue timeValue;

        QStringList strList = strLine.split(",");
        timeValue.m_dTime = strList[0].toDouble();
        timeValue.m_dValue = strList[1].toDouble();

        vecTimeValue.push_back(timeValue);
    }

    file.close();
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    WriteFile();
    ReadFile();

    return a.exec();
}

文件数据样式:
在这里插入图片描述

http://www.dtcms.com/a/129462.html

相关文章:

  • AMGCL库的Backends及使用示例
  • Java基础:Stream流操作
  • 【软考系统架构设计师】信息安全技术基础知识点
  • 25级总分413数学一142专业124东南大学820考研经验电子信息通信工程,真题,大纲,参考书。
  • 开源模型应用落地-qwen模型小试-Qwen2.5-7B-Instruct-tool usage入门-集成心知天气(二)
  • 深入理解 HTML5 语义元素:提升网页结构与可访问性
  • 【C++】中memcpy的使用
  • 校园AI体育:科技赋能教育,运动点亮未来
  • 【集成电路版图设计学习笔记】1. Introduction to Layout Design
  • k8s蓝绿发布
  • 软件系统安全设计方案,信息化安全建设方案(Word原件)
  • cookie、session、token、jwt、oauth
  • ESP32开发之ubuntu环境搭建
  • 岛屿问题——DFS、BFS
  • 高并发秒杀系统设计:关键技术解析与典型陷阱规避
  • 【Linux】Rhcsa复习 2
  • 基于51单片机语音实时采集系统
  • 仙剑奇侠传98柔情版游戏秘籍
  • 工业级安卓一体机在智能自助终端中的应用
  • Spark运行架构 RDD相关概念Spark-Core编程
  • 基于李永乐线性代数基础的行列式的起源于理解
  • Hqst的超薄千兆变压器HM82409S在Unitree宇树Go2智能机器狗的应用
  • 初步认识java
  • 关于香橙派OrangePi 5 Ultra 这个开源板子,开发Android
  • 通信算法之261: 时频分析- findpeaks 函数查找满足宽度要求的峰值
  • PyQt6基础_pyqtgraph_k线图缩放
  • 41、web前端开发之Vue3保姆教程(五 项目实战)
  • 足球比分分析页面可视化展示
  • ImportError: The ‘read_file‘ function requires the ‘pyogrio‘ or ‘fiona‘ package
  • 【深度学习与实战】3.1 逻辑回归模型