天天看點

C++每隔一段時間處理一些操作

最近在做日本的項目,今天一個需求是:

傳感器采集到的資料寫到一個TXT檔案中,每隔一個小時更換一個新的檔案寫入

時間控制的代碼如下:

#include <iostream>
#include <time.h> 
using namespace std;

int main()
{
	time_t start_time, end_time;
	time(&start_time);
	while (true)
	{
		time(&end_time);
		double cost = difftime(end_time, start_time);
		if (cost >= 5) //時間超過5s就退出循環
		{			
			std::cout << "cost : " << cost << " s" << std::endl;
			std::cout << "Time's up!  Stop!" << std::endl;
			break;
		}
		else
		{
			//to do things
			std::cout << "Doing things..." << std::endl;
		}
	}	
	system("pause");
	return 0;
}
           

繼續閱讀