//
// Created by funnywii on 2023/4/6.
// To save variables in a txt file
//
#include <fstream> // c++文件操作
#include <sys/time.h> // get time
#include <unistd.h>
#include <iostream>
#include <time.h>
using namespace std;
bool saveTxtFile(string filename,bool bIfClear, int iTimeSpan){
ofstream out_txt_file;
if (bIfClear == true){// clear the previous content
out_txt_file.open(filename, ios::out | ios::trunc);
}
else{ // reserve the previous content
out_txt_file.open(filename, ios::binary | ios::app | ios::in | ios::out);
}
out_txt_file << fixed;
struct timeval tv; //start time
time_t first,second;
first=time(NULL);
second=time(NULL);
// Header
out_txt_file << " Time " << " " << " Speed " << " " << " Direction " << std::endl;
// 保存数据到文件中
while(1)
{
second=time(NULL);
if(difftime(second,first)==iTimeSpan){
time_t now = time(0);
// 把 now 转换为字符串形式
tm *ltm = localtime(&now);
out_txt_file << ltm->tm_hour << ":" << ltm->tm_min << ":" << ltm->tm_sec << endl;
cout << "done" << endl;
first=time(NULL);
}
}
// 关闭文件
out_txt_file.close();
return true;
}