In Qt, the reading and writing of files can be realized by using various methods such as QFile
, QTextStream
and , etc. The following three methods are introduced respectively, and the specific codes for creating and reading files are given:QDataStream
1. Use QFile for file operations
How to create an
QFile
object and use this object to read the text file under the specified path, whereQFile
is the file operation class provided by Qt,readAll()
you can read the contents of the entire file by calling its member functions, it should be noted that when reading the file You need to open the file first, and finally close the file after reading, otherwise an error will be reported;
#include <QCoreApplication>
#include <QFile>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// 定义文件名
QString fileName = "C:\\cpp\\qt\\widget\\qq.txt";
// 创建文件对象
QFile file(fileName);
// 打开文件,并且以只读方式进行读取
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
// 读取文件内容
QByteArray fileData = file.readAll();
// 关闭文件
file.close();
// 输出读取的文件内容
qDebug() << "读取的文件内容:" << fileData;
} else {
// 如果文件打开失败,则输出错误信息
qDebug() << "打开文件失败!";
}
return a.exec();
}
2. Use QTextStream for text file operations
Create an
QTextStream
object and use this object to write data to the text file under the specified path. It should be noted that the file needs to be closed after writing the data, otherwise the data cannot be written to the file;
#include <QCoreApplication>
#include <QFile>
#include <QTextStream>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// 定义文件名
QString fileName = "C:\\cpp\\qt\\widget\\ww.txt";
// 创建文件对象
QFile file(fileName);
// 打开文件,并且以写入方式进行操作
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
// 绑定 QTextStream 对象和文件对象
QTextStream out(&file);
// 写入数据到文件中
out << "Hello World!";
// 关闭文件
file.close();
// 重新打开文件并以只读方式进行操作
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
// 读取文件内容
QString fileContent = in.readAll();
// 关闭文件
file.close();
// 输出读取的文件内容
qDebug() << "读取的文件内容:" << fileContent;
} else {
// 如果文件打开失败,则输出错误信息
qDebug() << "打开文件失败!";
}
} else {
// 如果文件打开失败,则输出错误信息
qDebug() << "打开文件失败!";
}
return a.exec();
}
3. Use QDataStream for binary file operations
Create an
QDataStream
object and use this object to write data to the binary file under the specified path. It should be noted that the file needs to be closed after writing the data, otherwise the data cannot be written to the file;
#include <QCoreApplication>
#include <QFile>
#include <QDataStream>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// 定义文件名
QString fileName = "C:\\cpp\\qt\\widget\\ee.txt";
// 创建文件对象
QFile file(fileName);
// 打开文件,并且以写入方式进行操作
if (file.open(QIODevice::WriteOnly)) {
// 绑定 QDataStream 对象和文件对象
QDataStream out(&file);
// 写入数据到文件中
out << QString("这是二进制文件");
// 关闭文件
file.close();
// 重新打开文件并以只读方式进行操作
if (file.open(QIODevice::ReadOnly)) {
QDataStream in(&file);
// 读取文件内容
QString fileContent;
in >> fileContent;
// 关闭文件
file.close();
// 输出读取的文件内容
qDebug() << "读取的文件内容:" << fileContent;
} else {
// 如果文件打开失败,则输出错误信息
qDebug() << "打开文件失败!";
}
} else {
// 如果文件打开失败,则输出错误信息
qDebug() << "打开文件失败!";
}
return a.exec();
}