Im trying to create a folder with todays date in c++. WHile in python its very easy , in c++ im finding it challenging. I tried this code where first I tried to print the date and then use the date values to create a folder. I even tried inserting unix commands but even those didnt work.
I simply want to create a directory like : DD_MM_YYYY or DD_MM_YY
#include <bits/stdc++.h>
#include <iostream>
#include <sys/stat.h>
#include <sys/types.h>
#include "time.h"
//#include "date/tz.h"
using namespace std;
using namespace std::chrono;
int main()
{ //option 1
time_t my_time =time(NULL);
cout << ctime(&my_time)<<endl;
///option 2
time_t t = time(NULL);
struct tm tm = *localtime(&t);
printf("now: %d-%d-%d %d:%d:%d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
printf("Date: %d_%d_%d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
// EXPERIMENT SECTION : ALL OF THESE THROWING ERRORS
//sub_fol=(tm.tm_year+1900).ToString()+"."+tm.tm_mon.ToString();
//sub_fol=tm.tm_mon;
string str="date +'%d_%m_%y'";
//str="mkdir "+ str;
const char *command=str.c_str();
//string dr=system(command);
//printf(string(tm.tm_mon));
// THIS ONES WORKING
if (mkdir("test_", 0777) == -1)
cerr << "Error : " << strerror(errno) << endl;
else
cout << "Directory created";
// THIS ONE ISNT WORKING
if (mkdir(system(command), 0777) == -1)
cerr << "Error : " << strerror(errno) << endl;
else
cout << "Directory created";
}