Simple string manip question

L

Lorn

I'm trying to add a file tpye to the end of a string as I'm doing some
file writing. This code doesn't work, what would be the correct way of
doing this:

ofstream a_file( myString + ".txt", ios::app );

Sorry for the triviality, I've been searching around all morning to no
avail. Thanks, for any help.

Lorn
 
S

Son of Sam

do this:
string a,b;
string bambi = a + b;

fstream a_file;
a_file.open(bambic_str(), ios_base::in | ios_base::eek:ut | ios::binary);
 
P

Phil Endecott

Lorn said:
I'm trying to add a file tpye to the end of a string as I'm doing some
file writing. This code doesn't work, what would be the correct way of
doing this:

ofstream a_file( myString + ".txt", ios::app );

What is myString? And how does your code "not work" - what error
message do you get?

If myString is a char*, then + doesn't work at all.

If myString is a string, then + will do what you expect and return a string.

But the ofstream constructor requires a char* for the filename
parameter, not a std::string (aarrgghh!). So you need to get a char*
for that temporary string:

(myString+".txt").c_str()

--Phil.
 
S

Son of Sam

do this:
string a,b;
string bambi = a + b;
fstream a_file;
a_file.open(bambic_str(), ios_base::in | ios_base::eek:ut | ios::binary);

here i meant: a_file.open(bambi.c_str(), ios_base::in | ios_base::eek:ut |
ios::binary);
(the dot before .c_str() )
 
L

Lorn

It was actually a CString. I ended up doing the code below, which works
fine. Thank you guys for your help.

CString name = "myName"
CString filetype = ".txt";
CString filename = name + filetype;
ofstream a_file ( filename, ios::app );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top