// copyfile1.cpp - Gets the name of a file from the user, 
//                 say it is "moo.dat". It copies it character
//                 by character to file "moo.datout".

#include <iostream>  // It allows us to use cin, cout
#include <fstream>   // It allows us to use ifstream, ofstream ..
#include <string>    // It allows us to use strings

using namespace std;

// Copy character by character the content of file fromfile
// to file tofile. Return 0 if success, -1 if cannot open
// fromfile, -2 if cannot open tofile.

int copyfilec(const char *fromfile, const char *tofile)
{
    // Open input file
    ifstream fin (fromfile);

    if ( !fin ) {
	   return (-1);
    }

    // Open output file
    ofstream fout (tofile);

    if ( !fout ) {
	   return (-2);
    }

    char current;        // The character just read
    while ( fin.get( current ) ){
       fout.put(current);
       cout.put(current);
    }

    fin.close();
    fout.close();
    return 0;
}


int main (void)
{
    // Read the name of the input file from standard input
    string inFName;  // Name of the input file
    cout << "Enter a File Name: ";
    cin >> inFName;

    // Compute the name of the output file
    string outFName = inFName + "out"; // Name of the output file


    //c_str() is a method that applied to a string returns
    //the corresponding C-string.
    int rc = copyfilec(inFName.c_str(), outFName.c_str());
	
    if (rc == -1)
	cout << "Cannot open " << inFName << endl;
    else if (rc == -2)
	cout << "Cannot open " << outFName << endl;
    else
	cout << "inFName is " << inFName << " outFName is " 
	     << outFName << endl;

    return 0;
}

