i have my file input
like this
ifstream inFile;
inFile.open(fileName.c_str());

and i cin >> fileName;
i want the output file name to be the input filename with .dat added to it
eg if fileName was c:/tmp/test.txt
i want the output to be c:/tmp/test.txt.dat

anyone know how i can do this everything ive tried isnt working

try this:

string filename;

cin >> filename;

filename += ".dat";

outFile.open(filename.c_str());

doesnt seem to be working

Try this:

#include <sstream>
string filename;
stringstream ss;
cin >> filename;
ss<<filename<<".dat";
outFile.open(ss.str().c_str());

The first str() gets the string from the string stream and the c_str() you already know.
(fill in the name of your own output file for outFile)

After further consideration I'm not sure why CP's solution would not work. What was the specific error you were getting?

(fill in the name of your own output file for outFile)

Was incorrect, my apologies. I had meant to say the name of your output filestream for outFile.

that worked thanks alot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.