c.str(), a concatenated string, and ofstream

string person;
string message;
string date = "11/23/12";
string time = "11:45 PM";

cout << "Who would you like to send a message to?";
getline(cin, person);

cout << "Please type the message that you would like to send: " << endl;
cout << "Please also note that pressing the enter key WILL send the message, so don't" << endl
<< "press enter unless you want to send the message." << endl << endl;
getline(cin, message);

string title = person + " " + date + " " + time + ".txt";

cout << title;

ofstream outfile;

outfile.open(title.c_str());
outfile << title << endl << endl << message;
//need to put who the message is from
outfile.close();



Why wont this work? I have done everything as usual, only changed the way that the title for the file was defined. The code itself works as I have changed title to just 'person' or just 'date' and it will write to that file. However, as soon as I concatenate two strings and try to use that, it doesn't write anything.


EDIT: changed "string title = person + " | " + date + " | " + time + ".txt";" to "string title = person + " " + date + " " + time + ".txt";"

Last edited on
What file system is this ? Usually a pipe character is not allowed in file name.
I have realized that, but I had added those at a later time to separate the variables and distinguish them, it did not work before that point.
Thanks for noticing. The file system is NTFS.
I have just tested it again and it still does not work. :/
Does this "works" ?

1
2
3
4
5
6
7
string title;
title += person;
title += " ";
title += date;
title += " ";
title += time;
title += ".txt";



Beware what "date" or "time" strings contains, a slash or drive separator is not allowed in a file name too.
Last edited on
>Beware what "date" or "time" strings contains, a slash or drive separator is not allowed in a file name too.

This is why! Thanks! Haha. I'm glad I posted here.

Also, why did you put " " around works?
Topic archived. No new replies allowed.