System time

how can i get the actual system time to cout?

# include <ctime> should be the library you need, then
long sys_time=time();

cout << sys_time;

that should do it but that might output the time in ticks... but the ctime library should have all the necessary functions.
i tried to implement ur example snippets in a function:

1
2
3
4
5
6
7
void addtolog(string logtext)
{
   ofstream logfile ( "logfile.txt",ios::app );
   long sys_time=time();
   logfile<<"\n"<<sys_time<<" "<<logtext;
   logfile.close;
}


but i get an error that says:
"too few arguments to function 'time_t time(time_t*)'"
closed account (3hM2Nwbp)

but i get an error that says:
"too few arguments to function 'time_t time(time_t*)'"


Have a read:

http://www.cplusplus.com/reference/clibrary/ctime/time/
thanks, this works now:

1
2
3
4
5
6
7
8
9
10
11
void addtolog(string logtext)
{
    time_t rawtime;
    struct tm * timeinfo;

    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    ofstream logfile ( "logfile.txt",ios::app );
    logfile<<"\n"<<asctime (timeinfo)<<logtext;

}
Topic archived. No new replies allowed.