Denis
Denis

Reputation: 758

Invalid argument error with printf and strftime

Let's say I have a program that writes something to the log file. Everything worked fine until I decided to add a timestamp to the filename.

Here's how i do this:

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");

I don't know why, but fopen returns NULL.

filename is okay: 20-08-2017_01:08:09.txt, but if I change the filename to logfile.txt for example - eveything works fine.

It's as if some characters were not allowed to be in the filename, but I tested it with -, _, : and that's not the case.

Does anybody have any ideas?

Upvotes: 0

Views: 1107

Answers (1)

Dominic Mazur
Dominic Mazur

Reputation: 46

Try escaping the : character, that might be the problem

Upvotes: 1

Related Questions