欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > Qt文件读写

Qt文件读写

2025/4/17 2:47:28 来源:https://blog.csdn.net/LoveXming/article/details/147073398  浏览:    关键词: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();
}

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com