Skip to main content
gamedev.net
🔒 Locked

C++: Declaring file

Started by DannyB 1 reply 250+ views
Original Post
DannyB
DannyB
When creating an ofstream for output, is it possible to have a variable as part of the file path? Such as this. String text1 = "parse.txt"; ofstream outPrimary ("n:\test\" && text1, ios::app); I've tried many different things, not really sure what to use to conjoin. [edited by - DannyB on April 29, 2004 10:03:40 AM]
CodeMunkie
CodeMunkie
Sure, just use the + operator on std::strings. I know the following will work:

std::string path = "n:\\test\\";
std::string filename = "parse.txt";

text1=path+filename;//now text1 holds "n:\\test\\parse.txt"


ofstream outPrimary(text1, ios::app);


[edited by - CodeMunkie on April 29, 2004 10:07:43 AM]
"When you die, if you get a choice between going to regular heaven or pie heaven, choose pie heaven. It might be a trick, but if it's not, mmmmmmm, boy."
How to Ask Questions the Smart Way.
nonpop
nonpop
Want to do it without a temporary file?

string filename = "file.txt";
ofstream f ((string ("n:\\path\\") + filename).c_str ());

I wouldn''t recommend it, though.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.