一、清空文本内容
QString text_path("G:\camSim_20210917\Library\G_codes.txt");
QFile file(text_path);
file.open(QFile::WriteOnly | QFile::Truncate);
file.close();
二、读取txt文件,按行读
void ReadWriteTxt::readTxt()
{
//读取文件
QFile file("./account.txt");
QTextStream stram(&file); //纯文本文件流
if(file.open(QFile::ReadOnly | QFile::Text))
{
while (!stram.atEnd())
{
QString line = stram.readLine();
}
file.close();
}
}
三、写入
void ReadWriteTxt::writeTxt()
{
QFile file(m_strTxtPath);
if(!file.open(QIODevice::WriteOnly |QIODevice::Text))
return;
QTextStream stream(&file);
stream << "111" << "练习" <<"\n";
stream << "222" << "练习" <<"\n";
file.close();
}