程序员开发实例大全宝库

网站首页 > 编程文章 正文

Qt 读写txt文本文件(qt读入文件)

zazugpt 2024-10-14 20:15:15 编程文章 28 ℃ 0 评论

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

需要导入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();

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表