Hi everyone,
can someone tell me how to write a program to display the current time and date, and copy to a text file whenever the program executed?
Thanks
Printable View
Hi everyone,
can someone tell me how to write a program to display the current time and date, and copy to a text file whenever the program executed?
Thanks
Look up some information on file I/O and C++'s built in time and date functions.
Read some tutorials on those. Read the libraries themselves.Code:<time.h>
<fstream.h>
Oh, and have fun :)
create a new win32 console application
insert this code in it.
Code:// we need the time and date
CTime t = CTime::GetCurrentTime();
// date
cout << "Date: " << t.GetYear() << "-";
if (t.GetMonth()<10)
cout << "0";
cout << t.GetMonth() << "-";
if (t.GetDay()<10)
cout << "0";
cout << t.GetDay() << "\r\n";
// time
cout << "Time: ";
if (t.GetHour()<10)
cout << "0";
cout << t.GetHour() << ":";
if (t.GetMinute()<10)
cout << "0";
cout << t.GetMinute() << ":";
if (t.GetSecond()<10)
cout << "0";
cout << t.GetSecond() << "\r\n";
getch();
you will need to include the proper library if the compiler ask for it.
BR
froque
Isn't CTime part of MFC. If it is you will also need to go to project -settings and select USING MFC IN STATIC LIBRARY. You will also have to include afxwin.h
Since when did the question concern MFC?
Not a single mention of MFC or even Windows. In the absence of such mention, let's try and stick with something portable, kay? :)Quote:
can someone tell me how to write a program to display the current time and date, and copy to a text file whenever the program executed?
Code:#include <iostream>
#include <fstream>
#include <ctime>
using namespace std;
int main(int argc, char *argv[])
{
if (argc == 2) {
ofstream log(argv[1]);
if (!log)
cerr<<"Logging disabled\n";
else {
time_t present = time(0);
log<< ctime(&present) <<endl;
log.close();
}
}
else if (argc > 1)
cerr<<"usage: $"<< *argv <<" <logfile>\n";
// Rest of the program
}
And, there's more date & time info in the Programming FAQ!