Using Variable as Name for Combined File

Hi,

I have been researching how to use the prefix of a user input file name as the prefix of an output file name for a combined file. I found this in an archived post by JLBorges on December 6, 2012, "Adding two text files into one text file", but the combined file name is entered directly, not from a variable.

1
2
3
4
5
std::ifstream file1( "file1.txt" ) ;
std::ifstream file2( "file2.txt" ) ;
std::ifstream file3( "file3.txt" ) ;
std::ofstream combined_file( "combined_file.txt" ) ;
combined_file << file1.rdbuf() << file2.rdbuf() << file3.rdbuf() ;


My code already has the user input file name as a variable. I would like to insert the prefix of that variable in line 4 with a new suffix.

I also found this code in an archived post by Duoas on May 31, 2010, "Changable filename based on user input," but I don't know how to adapt it to my combined file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
  {
  ofstream f;
  string filename;

  cout << "Please enter a file name to write: ";
  getline( cin, filename );

  f.open( filename.c_str() );
  f << "Hello world!\n";
  f.close();

  cout << "Good job! 'type' or 'cat' your new file to see what it says.\n";
  return 0;
  }


Thank you for your help,
MHLester
1
2
3
4
std::string filename;
std::cout << "Please enter a file name to write: ";
std::getline(std::cin, filename);
std::ofstream combined_file(filename + "_combined") ;
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>

std::string changeExtension(std::string name, const std::string& ext)
{
    std::size_t extPos = name.rfind('.') ;

    if ( extPos != std::string::npos )
        name.resize(extPos) ;

    return name + '.' + ext ;
}

int main()
{
    const std::string ext("ext") ;

    std::string myfilename ;
    while( std::cout << "Enter file name: " && 
           std::getline(std::cin, myfilename) && 
           myfilename.size() )
    {
        myfilename = changeExtension(myfilename, ext) ;
        std::cout << "New file name: \"" << myfilename << "\"\n\n" ;
    }
}
Enter file name: monkey
New file name: "monkey.ext"

Enter file name: radio.wav
New file name: "radio.ext"
Thank you, cire,

This has helped me with the filename.

MHenry
Topic archived. No new replies allowed.