Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reuse a string variable in c++

Tags:

c++

string

Is this correct, it works OK

string str("in.dat");
ifstream fin(str.c_str(), ios::binary | ios::ate );
.
.
.
//Do I need to clear the string before assigning new name???
str = "out.dat";
ofstream fout(str.c_str(), ios::binary); //seems to work

Regards


1 Answers

What you did is correct. The = operator will overwrite string contents, it's normal scenario of reusing string variable. It will even probably not realloc any buffer, but reuse existing instead.