1

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"; 

} 

1 Answers1

1

You can make use of std::localtime and std::puttime to get a formatted date. For example:

#include <iostream>
#include <chrono>
#include <sstream>
#include <iomanip>

int main()
{
    auto const now = std::chrono::system_clock::now();
    auto const in_time_t = std::chrono::system_clock::to_time_t(now);

    std::stringstream ss;
    ss << std::put_time(std::localtime(&in_time_t), "%d_%m_%Y");
        
    std::cout << ss.str() << std::endl;
}

Will get you:

14_01_2021

You can see https://en.cppreference.com/w/cpp/io/manip/put_time for more formatting options.


Then you can use mkdir or make use of std::filesystem if you are using C++17 or above.

#include <filesystem>
...
    std::filesystem::create_directory("abc");

See https://en.cppreference.com/w/cpp/filesystem/create_directory


For mkdir it expects a const char *path rather than a std::string but you can get a char pointer via c_str() e.g.:

mkdir(ss.str().c_str(), 0700);
0RR
  • 1,538
  • 10
  • 21
  • Tried this --> string str="mkdir ss.str()"; const char *command=str.c_str(); system (command); But is throwing error :- sh: 1: Syntax error: "(" unexpected – Anita Shukla Jan 14 '21 at 14:13
  • @AnitaShukla Are you aware what `system()` does? See https://en.cppreference.com/w/cpp/utility/program/system, also see https://stackoverflow.com/questions/7430248/creating-a-new-directory-in-c – 0RR Jan 14 '21 at 14:18
  • I tried this but still not working :-(, sorry but can you please help -----> mkdir(ss.str(), 0700); – Anita Shukla Jan 14 '21 at 14:24
  • @AnitaShukla try `mkdir(ss.str().c_str(), 0700);` as `mkdir` expects a `const char *path` rather than a `std::string` but you can get a char pointer via `c_str()` – 0RR Jan 14 '21 at 14:27