fstream没有创建一个附加到文件名的asctime值的文件

Bar*_*hor 2 c++ io

我已经调试了这一点,我知道这个问题,我只是不知道如何解决它.

现在我正在使用:

file.open(logFile.c_str(), std::ios::out | std::ios::app); 
Run Code Online (Sandbox Code Playgroud)

只要我注释掉下面标记的行,logFile变量就可以正常工作:

time_t rawtime;
struct tm * timeinfo;
time (&rawtime);
timeinfo = localtime (&rawtime);
logFile = "bin/";
if(_DEBUG) { logFile += "Debug/"; }
else { logFile += "Release/"; }
logFile += fileName;
if(_DEBUG) { logFile += "Debug-"; }
else { logFile += "Release-"; }
logFile += asctime(timeinfo); // Works fine with this line commented.
logFile += ".log";
Run Code Online (Sandbox Code Playgroud)

那么如何才能让这条线路正常工作呢?

Col*_*lin 6

冒号(:)是文件名的非法字符,至少对于NTFS而言.请尝试使用strftime()格式化不包含冒号的字符串.