Though it's valid C++, it's not very nice C++.
The reader of your code needs to remember that the str
variable is mutable, and serves a different purpose throughout your code file. When code is inserted between the second assignment of str and it's use as a filename, the reader may find it hard to find out what's in the variable.
It's always better to give your variables a sensible name; it then almost always becomes constant.
const char* inputpath("in.dat");
ifstream inputstream( inputpath, ... );
const char* outputpath("out.dat");
... lots of code
ofstream outputstream( outputpath, ... );