C + + has four commonly used pre-defined names, namely: __file__,__line__,__date__,__time__
__FILE__: Path plus name of the record file
__LINE__: The number of rows that the record file has been compiled
__DATE__: Compile date for log files
__TIME__: Compile time for log files
Can be used as a variable directly, generally used as a program debugging
Example:
#include <iostream>
using namespace Std;
int main ()
{
cout << "File =" << __file__ << ' \ n '
<< "line =" << __line__ << ' \ n '
<< "DATE =" << __date__ << ' \ n '
<< "time =" << __time__
<< Endl;
GetChar ();
}
Operation Result:
File = G:/program/study/c++/test1.cpp
line = 17
DATE = May 27 2004
Time = 09:59:01
For printing logs, debugging is very convenient
The following function is the time type to convert the __DATE__ macro to time_t for easy operation
Static time_t Getdatefrommacro (CharConst *time) {
Char s_month[5];
int month, day, year;
struct tm t = {0};
static const Char month_names[] = "Janfebmaraprmayjunjulaugsepoctnovdec";
sscanf (Time, "%s%d%d", S_month, & Day, &year); Month = (strstr (Month_names, S_month)-month_names)/3;
T.tm_mon = month;
t.tm_mday = day;
t.tm_year = year-1900;
T.TM_ISDST =-1;
return mktime (&t);
}
__file__,__line__,__date__,__time__ predefined names commonly used in C + +