在上节中,我们实现了文件夹和文件的创建和文件内容的添加,本节介绍下系统时间的获取方法。
系统时间在嵌入式开发中有着重要的意义,从一些事件的时间特性上可以追踪到程序的运行状态和运行特性。比如有一个异常操作,如果记录下了操作时间和操作动作,那么可以追踪到什么原因引起的系统异常。
在上节程序的基础上添加系统获取语句,代码源程序如下:
#include <QCoreApplication>
#include <QtDebug>
#include <QDir>
#include <QString>
#include <QFile>
#include <QTextStream>
#include <QDateTime>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// QString mStr = "hello,www.eepw.com.cn";
QDir Mkpath;
QString myPath="/home/mjl/QT_exam/lesson2/data";
if( !Mkpath.exists(myPath))
{
Mkpath.mkpath(myPath);
qDebug() << Mkpath.exists(myPath);
}
qDebug()<<"the path is exist!";
QFile file("/home/mjl/QT_exam/lesson2/data/data.txt");
if(file.exists())
{
qDebug()<<"creat success!";
}
file.open(QIODevice::ReadWrite | QIODevice::Text);
QTextStream out(&file);
QDateTime time = QDateTime::currentDateTime();
myPath = time.toString("yyyy-MM-dd_hh-mm-ss");
out << "hello,eepw";
out << myPath;
file.flush();
file.close();
return a.exec();
}
我要赚赏金
