假设我有一个向日志文件写入内容的程序。一切正常,直到我决定为文件名添加时间戳。
这是我的做法:
time_t rawtime;
struct tm * timeinfo;
char buffer[80];
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer, sizeof(buffer), "%d-%m-%Y_%I:%M:%S_", timeinfo);
std::string timestring(buffer);
std::string filename = timestring + ".txt";
std::cout << "\n" << filename << std::endl;
FILE *f = fopen(filename.c_str(), "w");
我不知道为什么,但是 fopen
返回 NULL
。
filename
没问题:20-08-2017_01:08:09.txt
,但如果我将文件名更改为 logfile.txt
例如 - 一切正常。
好像有些字符不允许出现在文件名中,但是我用-
, _
, :
测试了一下,就是这样不是这样的。
有没有人有什么想法?
最佳答案
尝试转义 : 字符,这可能是问题所在
关于c++ - printf 和 strftime 的无效参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45776774/