0

I have this code which should create & open a file.

OpenFile():

std::string fileName = GetCurrentDate() +".txt";
std::cout << fileName << std::endl; //debug
m_file.open(fileName);

if (!m_file.is_open()) {
    LOG(0, "ERROR creating log file");
}

The log file's name should be the current date. (If I use a hardcoded text like "log.txt" it works.)

GetCurrentDate():

struct tm newtime;
__time32_t aclock;
_time32(&aclock);
_localtime32_s(&newtime, &aclock);

char buffer[32];
errno_t errNum;
errNum = asctime_s(buffer, 32, &newtime);

if (errNum) {
    printf("Error code: %d", (int)errNum);
}

std::string str = buffer;
//buffer ends with an end line
str.erase(std::remove(str.begin(), str.end(), '\n'), str.end());

return str;

The result fileName string is this: Tue Dec 27 03:29:17 2016.txt

Why doesn't it open the file?

(extra: isn't there a simpler way of getting current date? Y-M-D is enough for me)

(I use Visual Studio which recommended this)

Tudvari
  • 2,715
  • 2
  • 14
  • 33
  • 2
    You can't use `:` in a filename on Windows. The two `:` in the filename (in the time portion) are making the filename invalid. – Ken White Dec 27 '16 at 02:39
  • Thanks, you can write an answer if you want to, and I will accept it in the morning, but now I have to sleep :) Thanks again! – Tudvari Dec 27 '16 at 02:50
  • For the extra question: http://stackoverflow.com/questions/997946/how-to-get-current-time-and-date-in-c – Danh Dec 27 '16 at 03:31

0 Answers0