printf 和 strftime 的参数错误无效

Invalid argument error with printf and strftime

本文关键字:错误 无效 参数 strftime printf      更新时间:2023-10-16

假设我有一个程序将某些内容写入日志文件。一切正常,直到我决定在文件名中添加时间戳。

这是我这样做的方法:

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例如 - 一切都可以正常工作。

就好像文件名中不允许某些字符,但我用-_:对其进行了测试,事实并非如此。

有人有什么想法吗?

尝试转义 : 字符,这可能是问题所在