Hi, MiiniPaa
Here is my modified code.
1 2 3 4 5 6 7 8 9 10
|
std::string newfilename;
std::cout << "Choose new file name:";
std::getline(std::cin, newfilename );
ifstream file1("New2.TXT");
ifstream file2("New.TXT");
std::ofstream combined_file(newfilename + ".txt");
combined_file << file1.rdbuf() << file2.rdbuf() << ")";
file1.close();
file2.close();
|
I can't seem to get this to work with a variable. It will not compile for me (see error message below). Though it works fine if I enter a name like "Filename.txt" instead of a variable, and then I don't need the first three lines of code, of course.
error: no matching function for call to 'std::basic_ofstream<char>::basic_ofstream(std::string&)'|
If I change the code on line 7 slightly to the following, it will compile and run fine, and I don't need to add a suffix, because the user will supply that.
1 2 3 4 5 6 7 8 9 10
|
std::string filename;
std::cout << "Choose new file name:";
std::getline(std::cin, newfilename );
ifstream file1("New2.TXT");
ifstream file2("New.TXT");
std::ofstream combined_file(newfilename.c_str());
combined_file << file1.rdbuf() << file2.rdbuf() << ")";
file1.close();
file2.close();
|
However, the user entered a file name earlier in the program, and I want to use that file name, so I don't have to ask the user to enter another file name.
Here is the earlier code I used to get the file and file name from the user.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
std::string XYZ;
ifstream myfile;
ofstream copyfile ("Copy.TXT");
cout << "Please enter the input file name and location> " << flush;
while (true)
{
myfile.close();
myfile.clear();
string myfilename;
getline( cin, myfilename );
myfile.open( myfilename.c_str() );
if (myfile) break;
cout << "Invalid file. Please enter a valid input file name and location> " << flush;
|
I would like to use "myfilename" as the variable for the prefix, and change the suffix to ".txt" for the combined file. I tried the following code, which compiles, but only ".txt" prints as the file name. If it worked as expected, I think it would come out like "originalfile.nfo.txt, and I want just "originalfile.txt" as the result. Line 6 of the following code does not print out anything either, so I know I am not properly calling myfilename in this part of my code.
1 2 3 4 5 6 7 8 9 10 11 12 13
|
myfile.close();
myfile.clear();
string myfilename;
getline( cin, myfilename );
myfile.open( myfilename.c_str() );
cout << myfilename;
ifstream file1("New2.SGF");
ifstream file2("New.SGF");
std::ofstream combined_file((myfilename+".SGF").c_str());
combined_file << file1.rdbuf() << file2.rdbuf() << ")";
file1.close();
file2.close();
|
Thanks for your help.
MHenry