读:
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 }