c++ - Creating a text file with a timestamp in the title -
i'm trying write program outputs lot of data in separate text files. want title of each file poker_log_timestamp_datestamp.txt however, doesn't create file, nor throw errors!
here's code:
#include <fstream> #include <iostream> #include <ctime> #include <string> using namespace std; int main(){ char sdate[9]; char stime[9]; fstream log; _strdate_s(sdate); _strtime_s(stime); string filename = "poker_log_"; filename.append(stime); filename.append("_"); filename.append(sdate); filename.append(".txt"); cout<<"filename == "<<filename<<endl; log.open(filename.c_str()); log<<"\n\nhuman won tournament.\n"; log.close(); system("pause"); } how make work? 1 other thing: if comment out filename.append(stime) , filename.append(sdate), works fine.
solved :d file name cant have slashes or colons, replaced them both dashes. here working code:
#include <fstream> #include <iostream> #include <ctime> #include <string> #include <cstdio> using namespace std; int main(){ char sdate[9]; char stime[9]; ofstream log; _strdate_s(sdate); _strtime_s(stime); string filename = "poker_log_"; filename.append(stime); filename.append("_"); filename.append(sdate); filename.append(".txt"); for(int = 0; i<filename.length(); ++i){ if (filename[i] == '/' || filename[i] == ':') filename[i] = '-'; } log.open(filename.c_str()); if (log.fail()) perror(filename.c_str()); log<<"\n\nhuman won tournament.\n"; log.close(); system("pause"); }
the date , time strings may have characters in them (like colons) may not legal characters file system.
Comments
Post a Comment