每天总结一点点,成长一点点~~
1.头文件
#include <ctime>
/* get system current time*/ char* getSysTime(){ time_t now; struct tm *timenow; time(&now); timenow = localtime(&now); return asctime(timenow); }
2.QT下 头文件#include <QDateTime>
QDateTime time; timeLabel->setText(time.currentDateTime().toString("yyyy") + "年 " + \ time.currentDateTime().toString("M") + "月 " + \ time.currentDateTime().toString("d") + "日 " + \ time.currentDateTime().toString("h") + "点 " + \ time.currentDateTime().toString("m") + "分 " + \ time.currentDateTime().toString("s") + "秒" );
3.shell命令
system("while : ; do clear; date; sleep 1; done");
本想获取该shell命令的输出的,结果没成功~我猜是因为不确定命令没有跑完,是不能捕获到输出的
所以直接用 "date"就输出成功了。。。
参考:http://www.linuxidc.com/Linux/2011-04/34092.htm
用的是:
void executeCMD(const char *cmd, char *result) { char buf_ps[1024]; char ps[1024]={0}; FILE *ptr; strcpy(ps, cmd); if((ptr=popen(ps, "r"))!=NULL) { while(fgets(buf_ps, 1024, ptr)!=NULL) { strcat(result, buf_ps); if(strlen(result)>1024) break; } pclose(ptr); ptr = NULL; } else { printf("popen %s error\n", ps); } }