如何在c ++中重用字符串变量

2 c++ string

这是正确的,它可以正常工作

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
Run Code Online (Sandbox Code Playgroud)

问候

小智 6

其他人所说的都是真的.但是,在您发布的代码中,您也可以说:

ifstream fin( "in.dat", ios::binary | ios::ate );
ofstream fout( "out.dat", ios::binary ); 
Run Code Online (Sandbox Code Playgroud)