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();
}
文件數據樣式: