一、按照时间生成文件,并写入数据
//创建文件夹
QString strTime;
QString dirPath;
QDateTime time = QDateTime::currentDateTime();
strTime = time.toString("yyyy-MM-dd");
dirPath = QCoreApplication::applicationDirPath() + "/" + str_time;
QDir dir;
if(!dir.exists(dirPath)){
dir.mkdir(dirPath);
}
//创建文件,并进行文件读写
QString str_time;
QString filepath;
QDateTime time = QDateTime::currentDateTime();
str_time = time.toString("yyyy-MM-dd_hh-mm-ss");
filepath = dirPath + "/" + str_time + ".txt";
QFile* file;
file = new QFile(filepath);
if(!file->open(QIODevice::WriteOnly | QIODevice::Append)){
QMessageBox::warning(NULL, "warning", "数据存储文件创建失败", QMessageBox::Yes, QMessageBox::Yes);
return;
}
QTextStream txtInput(file);
txtInput << "你所需要写入的数据"<< "\r\n";
file.close();