I wanted to make an Attendance system which would take system date and time as file name of the file for ex: this is how normally it is
int main () {
time_t t = time(0); // get time now
struct tm * now = localtime( & t );
cout << (now->tm_year + 1900) << '-'
<< (now->tm_mon + 1) << '-'
<< now->tm_mday
<< endl;
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
but i want system date and time in place of example.txt i have calculated time by including ctime header file in the program above program is just example .
You can use strftime()
function to format time to string,It provides much more formatting options as per your need.
int main (int argc, char *argv[])
{
time_t t = time(0); // get time now
struct tm * now = localtime( & t );
char buffer [80];
strftime (buffer,80,"%Y-%m-%d.",now);
std::ofstream myfile;
myfile.open (buffer);
if(myfile.is_open())
{
std::cout<<"Success"<<std::endl;
}
myfile.close();
return 0;
}