一、逐行读文本
#include <QDebug>
#include <QFile>
void ReadLine()
{
QFile file("要读的文件路径");
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
while (!file.atEnd())
{
QByteArray line = file.readLine();
QString str(line);
qDebug() << str;
displayString << str;
}
file.close();
}
}
二、创建文本文件 + 逐行写
void WriteLine()
{
QString text_path("G:\\camSim_20210917\\Library\\G_codes.txt");
QFile file(text_path);
if (file.open(QIODevice::ReadWrite | QIODevice::Text))
{
QTextStream stream(&file);
stream.seek(file.size());
for (int i = 0;i<10;i++)
{
QString G_string("G01 ");
QString X_string = QString::number(i);
stream << G_string + X_string << "\n";
}
file.close();
}
}
三、清空文本内容
QString text_path("G:\\camSim_20210917\\Library\\G_codes.txt");
QFile file(text_path);
file.open(QFile::WriteOnly | QFile::Truncate);
file.close();