QT写TXT文件
Qt
代码:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 
 | #include <QDir>
 #include <QDateTime>
 #include <QDebug>
 
 void MainWindow::on_saveBtn_clicked()
 {
 
 QDateTime da_time;
 QString time_str = da_time.currentDateTime().toString("yyyy-MM-dd HH-mm-ss");
 
 
 
 
 QDir *DataFile = new QDir;
 bool exist = DataFile->exists("DataFile");
 if(!exist)
 {
 bool isok = DataFile->mkdir("DataFile");
 if(!isok)
 QMessageBox::warning(this,"sdf","can't mkdir",QMessageBox::Yes);
 }
 QString fileName = "DataFile/"+time_str+"datafile.txt";
 QString str = "this is testing for save data to txt file by Qt programming.";
 QFile file(fileName);
 if(!file.open(QIODevice::WriteOnly|QIODevice::Text|QIODevice::Append))
 {
 QMessageBox::warning(this,"sdf","can't open",QMessageBox::Yes);
 }
 QTextStream stream(&file);
 stream<<time_str+":"+str<<"\n";
 file.close();
 }
 
 | 
原文地址:https://www.cnblogs.com/AI-Algorithms/p/3744007.html