// ffile1.cpp -- Gets the name of a file from the user, say it is "moo.dat".
//               It copies it to file "moo.datout", changing all letters 
//               to capitals and replacing streaks of whitespaces by a single 
//               space.
//               We use C strings, i.e. character arrays terminated by '\0'
//

#include <iostream> // It allows us to use cin, cout
#include <fstream>  // It allows us to use ifstream, ofstream
#include <cstring>  // It allows us to use strcpy, strcat, strcmp, ..
#include <cctype>   // It allows us to use toupper, isspace, ..


int main (void){
    const int MAXSIZE = 256;// This is the maximum size we allow
                            // to input strings
    char inFName[MAXSIZE];  // Name of the input file
    char outFName[MAXSIZE]; // Name of the output file

    // Read the name of the input file from standard input
    cout << "Enter a File Name: ";
    cin >> inFName;  // This reads from first nonwhite character to 
		// first white character into inFName and adds at end '\0'

    // Compute the name of the output file
    strcpy(outFName, inFName);
    strcat(outFName, "out");   // If inFName is "moo", outFName becomes "mooout"

    ifstream fin;
    fin.open(inFName); // This is a way to open a file
    if (fin.fail()){
	cout << "Cannot open " << inFName << endl;
	exit(1);
    }
    ofstream fout(outFName); // This is another way to open a file
    if (fout.fail()){        // Here I could have written just "if (!fout) {"
	cout << "Cannot open " << outFName << endl;
        exit(1);
    }

    char current;
    char previous = 'a';
    while (fin.get(current)) { // fin.get(current) returns a new stream value that is null
			       // if we are at the end of file.
                               // You can also test if fin is at end of file with the method fin.eof().
       if ((!isspace(current)) || (!isspace(previous))  || (current == '\n')){
            // isspace returns a non zero value iff its argument is a space
            // character, that is something like ' ', '\t', '\n'.
          current = toupper(current); // toupper converts a letter to uppercase
            // and leaves all other characters unchanged.
          fout.put(current);
          cout.put(current);
       }
       previous = current;
    }
    fin.close();	// It is polite to close what you have opened
    fout.close();	// "  "  "      "  "     "    "   "    "

    return 0;
}



