int main() { //testing program //string timenow = getDateTime(); //string timenow = getDate(); string timenow = getMonth(); cout << "time now is" << timenow << endl; //testWriteAtEnd("testINDSDFDSFSG"); //testWriteAtEnd(timenow); //createDirectory("testagain\\AGAIN"); //createDirectory(string(timenow)); //createDirectoryHierachy(timenow, '_'); //createDirectoryHierachy("2017_08_07", '_'); createDirectoryHierachy("2017/08/09", '//'); //Test(timenow); //Test("asdf\\efgh\\zxcv"); cout << "done" << endl; getchar(); return 0;}void testWriteAtEnd(string str) { cout << "trying to append at end of file" << endl; ofstream file; /* * std::ios_base::app: Locate cursor to EOF when writing. If path not exist, create it. */ file.open("file.txt", std::ios_base::app); //open a file file << str << endl; //write to it file.close(); //close it cout << "appended" << endl;}void createDirectory(string dir_name) { cout << "trying to create folder" << endl;/* * access()/_access(): return 0 if exist folder in pwd and -1 if not * mkdir()/_mkdir(): return 0 if success and -1 if not */#if defined(_WIN32) if (_access(dir_name.c_str(), 0) == -1) { cout << dir_name << " not existing" << endl; if (_mkdir(dir_name.c_str()) == 0) { cout << dir_name << " existing now" << endl; } else { cout << dir_name << " cannot be created" << endl; } } else { cout << dir_name << " already existed. No need to create" << endl; }#else if (access(dir_name.c_str(), 0) == -1) { cout << dir_name << " not existing" << endl; if (mkdir(dir_name.c_str(), 0777) == 0) { cout << dir_name << " existing now" << endl; } else { cout << dir_name << " cannot be created" << endl; } } else { cout << dir_name << " already existed. No need to create" << endl; }#endif}void createDirectoryHierachy(string dir_name, char splitter) { char delimiter; #if defined(_WIN32) delimiter = '\\'; #else delimiter = '/'; #endif cout << "trying to create folder by hierachy" << endl; std::string folder_builder; std::string sub; sub.reserve(dir_name.size()); for (auto it = dir_name.begin(); it != dir_name.end(); ++it) { //cout << *(folder.end()-1) << endl; const char c = *it; sub.push_back(c); if (c == splitter) { sub.pop_back(); sub.push_back(delimiter); //append / at end of file name, program will create and get into this folder } if (c == splitter || it == dir_name.end() - 1) { folder_builder.append(sub); createDirectory(folder_builder); sub.clear(); } //if (c == '_') //{ // sub.push_back('\\'); // folder_builder.append(sub); // createDirectory(folder_builder); // sub.clear(); //} //else if (it == dir_name.end() - 1) { // //cout << "here" << endl; // sub.push_back(c); // folder_builder.append(sub); // createDirectory(folder_builder); // sub.clear(); //} //else //{ // sub.push_back(c); //} }}string getDateTime() { char timenow[20]; time_t now = time(NULL);#if defined(_WIN32) struct tm p; localtime_s(&p, &now); strftime(timenow, 20, "%Y_%m_%d_%H%M%S", &p);#else struct tm *p; p = localtime(&now); strftime(timenow, 20, "%Y_%m_%d_%H%M%S", p);#endif return string(timenow);}string getDate() { char timenow[20]; time_t now = time(NULL);#if defined(_WIN32) struct tm p; localtime_s(&p, &now); strftime(timenow, 20, "%Y_%m_%d", &p);#else struct tm *p; p = localtime(&now); strftime(timenow, 20, "%Y_%m_%d", p);#endif return string(timenow);}string getMonth() { char timenow[20]; time_t now = time(NULL);#if defined(_WIN32) struct tm p; localtime_s(&p, &now); strftime(timenow, 20, "%Y_%m", &p);#else struct tm *p; p = localtime(&now); strftime(timenow, 20, "%Y_%m", p);#endif return string(timenow);}