특정 시간에 프로그램을 실행하려면 cron
또는 Windows 작업 스케줄러와 같은 외부 도구를 사용해야합니다. 코드 이 실행하고 당신은 그냥 약간의 특정 시간까지 작업을 지연 할 경우 이미
:-) 실행되어 있지 않은 경우 프로그램 자체를 실행할 수 없습니다, 그것은 ctime
헤더에있는 모든 재료가 무엇인지입니다.
time()
및 localtime()
을 사용하여 로컬 시간을 struct tm
으로 가져 와서 특정 시간이 현재인지 확인하기 위해 필드를 검사 할 수 있습니다. 그렇다면 조치를 취하십시오. 그렇지 않은 경우 루프를 돌리고 다시 시도하십시오 (필요한 경우 적절한 지연이 있음).
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int main() {
time_t now;
struct tm *tstr;
// Ensure first one is printed.
int lastSec = -99;
// Loop until time call fails, hopefully forever.
while ((now = time(0)) != (time_t)-1) {
// Get the local time into a structire.
tstr = localtime(&now);
// Print, store seconds if changed and multiple of five.
if ((lastSec != tstr->tm_sec) && ((tstr->tm_sec % 5) == 0)) {
cout << asctime(tstr);
lastSec = tstr->tm_sec;
}
}
return 0;
}
Stackoverflow에 오신 것을 환영합니다! 사람들이 일찍 문제를 해결하고 도움을 줄 수 있도록 코드 등의 노력을 기울여 질문을 자세히 작성해 주실 수 있습니까? 감사! – JRSofty