c++获取系统当前时间写入txt文件

头文件:iomanip
代码实践:

#include <iomanip>      //获取系统当前时间头文件
using namespace std;
//存入每次检测到的目标数据txt文件追加模式
int main(){
	ofstream outfile;
	SYSTEMTIME sys;       // 定义一个 SYSTEMTIME 结构体 sys
	outfile.open("footlog.txt", ios::in || ios::trunc);
	if (!outfile) cout << "footlong.txt error " << endl; //文件打开错误
while(1){
if (irbuff[0]){
			
GetLocalTime(&sys);   // GetLocalTime是一个Windows API 函数,用来获取当地的当前系统日期和时间。
outfile << std::setfill('0') << std::setw(2) << sys.wHour
				<< ":" << std::setfill('0') << std::setw(2) << sys.wMinute << ":" << std::setfill('0') << std::setw(2) << sys.wSecond;
outfile << "1:" << irbuff[0] << "," << irbuff[1] << " " << endl;
		}
}
outfile.close();         //关闭文件
return 0;
}

运行结果:

前面是小时:分:秒,后面是我自己测的的数据。
至于if语句内容,自己根据需求修改。

一键三连呀!
原文地址:https://www.cnblogs.com/jee-cai/p/14095311.html