Search code examples
c++filestreamofstream

Cannot create a file name including date and time in C++


I have this:

std::ofstream * file = new std::ofstream();
if (seperator == 1) file->open("C:/Users/Max/Desktop/Sparade filer/" + name + ".txt");
else 
{
    std::string stamp = (std::string)__DATE__ + " : " + (std::string)__TIME__;
    file->open("C:/Users/*directory*/" + name + " - " + stamp + ".txt");
}

*file << data;
delete file;

*directory* is of course something else

But for some reason, only the things within the if-block works. The filename doesn't exist, so a new file is created completely based on the variables (name is equal to "my save" in this case). So if I set my seperator to 1, I get a new file in my directory called "my file.txt", by which means that it functions properly. However as soon as I set it to something else, I don't get anything. I've checked several times, and the else block is entered, and the first argument of ofstream::open() is a valid string.

Help would be gladly appreciated!


Solution

  • A filename is not allowed to contain character ":".