How do i make a program display the time in a txt file every 2min?

I want to make a program that displays the time every 2 min in a txt file but idk how to do it.I wrote this but it doesn't do the desired effect it only writes the time once.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  #include <time.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <time.h>
#include <windows.h>

using namespace std;

void Time();



int main ()
{
    time_t rawtime;
    time (&rawtime);

    ofstream myfile;
    myfile.open ("example.txt");
    myfile << "The current local time is: " << ctime (&rawtime) << endl;
    myfile.close();

    Time();
    Sleep(200);
    Time();




}

void Time()
{
    time_t rawtime;
    time (&rawtime);

         ofstream myfile;
    myfile.open ("example.txt");
    myfile << "The current local time is: \n" << ctime (&rawtime) << endl;
    myfile.close();

}
closed account (Dy7SLyTq)
if your on linux: just cron it. if your on windows: i have a console hide function i think ( i cant remember if i got rid of it) and you can just have it continuosly run in the background and have it write to file every 2 mins
Yeah i would like it to.I want the time to be written in the file like.

1:43AM

*2 min later*

1:44AM

and all 2 times still there.

Thats what i want.Any idea how to go about doing that?
closed account (Dy7SLyTq)
are you on linux or windows?
since your code will exit after you write it.
several problems,
1. the Sleep is in milliseconds, 1 second = 1000 milliseconds, so Sleep(2000)
2. write two calls of Time() function cannot help to run it forever, you can try
while(true)
{
Time();
Sleep(2000);
}
instead
3. you can also try to use for in windows cmd, for /L %i in (0,0,1) do (your_app.exe)
I am on Windows.

Hzj jie i want it to keep writing the time every so minutes. until the program is closed.
Topic archived. No new replies allowed.