龙空技术网

Qt 读写txt文本文件

C加加Qt技术开发老杰 310

前言:

如今咱们对“qt 写文本文件”都比较关注,小伙伴们都想要了解一些“qt 写文本文件”的相关资讯。那么小编也在网上搜集了一些有关“qt 写文本文件””的相关资讯,希望小伙伴们能喜欢,朋友们一起来了解一下吧!

打开文件时,使用参数选择打开文件模式

需要导入QFile和qDebug、QString头文件

写入覆盖写入

 QFile f("D:\\qtManager.txt"); if(!f.open(QIODevice::WriteOnly | QIODevice::Text)) {     qDebug() << ("打开文件失败"); } QTextStream txtOutput(&f); QString str = "123"; txtOutput << str << endl; f.close();
文末写入
 QFile f("D:\\qtManager.txt"); if(!f.open(QIODevice::ReadWrite | QIODevice::Append))   //以读写且追加的方式写入文本 {     qDebug() << ("打开文件失败"); } QTextStream txtOutput(&f); QString str = "123"; txtOutput << str << endl; f.close();
读取
  QFile f("D:\\qtManager.txt");  if(!f.open(QIODevice::ReadOnly | QIODevice::Text))  {      qDebug() << ("打开文件失败");  }  QTextStream txtInput(&f);                   QString lineStr;  while(!txtInput.atEnd())  {     lineStr = txtInput.readLine();     qDebug() << (lineStr); } f.close();

标签: #qt 写文本文件