-1

I feel like I've tried everything, I can get the first file to append to the second but cannot get the second file into a third. What am I doing wrong?

To be clear I need to take one file, append it to a second file, then put the contents of that second file into a third. I was able to simulate this outcome by putting both files into strings and then putting those strings into a third file, but that's not 'correct' in this problem.

I'm not particular to any way or any technique, I've tried a few and nothing works. This is the latest attempt, still doesn't work for the last step.

Here's my code:

#include <iostream>
#include <string> 
#include <fstream>

using namespace std;

int main()
{
    string a,b,c;
    cout << "Enter 3 file names: ";
    cin >> a >> b >> c;
    fstream inf;
    ifstream two;
    fstream outf;

    string content = "";
    string line = "";
    int i;
    string ch;

    inf.open(a, ios::in | ios:: out | ios::app);
    two.open(b);
    outf.open(c, ios::in);

    //check for errors
if (!inf)
    {
    cerr << "Error opening file" << endl;
    exit(1);
     } 
if (!two)
    {
    cerr << "Error opening file" << endl;
    exit(1);
    } 
if (!outf)
    {
    cerr << "Error opening file" << endl;
    exit(1);
    } 
 for(i=0; two.eof() != true; i++)
        content += two.get();

    i--;
    content.erase(content.end()-1);
    two.close();

    inf << content;
    inf.clear();
    inf.swap(outf);


    outf.close();
    inf.close();
    return 0;
  • 2
    You should read [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). While it's for C the basic premises is the same in C++. – Some programmer dude Mar 29 '16 at 12:18
  • 2
    @JoachimPileborg We do have a C++ version at [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – NathanOliver Mar 29 '16 at 12:20
  • @NathanOliver Considering that I made a small edit to the accepted answer just a few days ago you could think I would have remembered it... Thanks for reminding me :) – Some programmer dude Mar 29 '16 at 12:22
  • @JoachimPileborg Oh yeah look at that :) – NathanOliver Mar 29 '16 at 12:23
  • Thanks both of you, as a newbie I had no idea about this concept! I thought "while(!eof(file))" Was actually doing something. Thanks again. – George McFlying Mar 29 '16 at 13:57

1 Answers1

0

Here's an idea:

#include <fstream>
using namespace std;

void appendf( const char* d, const char* s )
{
  ofstream os( d, ios::app );
  if ( ! os )
    throw "could not open destination";

  ifstream is( s );
  if ( ! is )
    throw "could not open source";

  os << is.rdbuf();
}

int main()
{
  try
  {
    appendf( "out.txt", "1.txt" );
    return 0;
  }
  catch ( const char* x )
  {
    cout << x;
    return -1;
  }
}
zdf
  • 3,363
  • 2
  • 14
  • 25
  • I had the thought last night right before sleep of doing it with a function, thanks for reminding me! This looks promising I'll try it out. – George McFlying Mar 29 '16 at 13:56