Qt txt文件读写

读:

 1 void MainWindow::ReadTxt(QString filePath)
 2 {
 3     QFile file(filePath);
 4     if (file.open(QIODevice::ReadOnly | QIODevice::Text))
 5     {
 6         while (!file.atEnd())
 7         {
 8             QByteArray line = file.readLine();
 9             QString str(line);
10         }
11 
12         file.close();
13     }
14 }

 

写:

 1 void WriteTxt(QString filePath, QString txt)
 2 {
 3     QFile file(filePath);
 4     if (file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Append))
 5     {
 6         QTextStream stream(&file);
 7         stream << txt << "\n";
 8         file.close();
 9     }
10 }

 

posted @ 2021-01-04 15:30  阳光下的小土豆  阅读(529)  评论(0编辑  收藏  举报