// Program to copy one file to another — version 2
/*This is the same program we looked at a couple weeks ago with the exceptio
 *this one uses command-line arguements rather than printf and scanf*/


#include <stdio.h>

int main (int  argc, char  *argv[])
{

    /*Create two file pointers for files you will list on the command
     *line*/
    FILE  *in, *out;
    int   c;

    /*You should always check to see check to make sure the correct number
     *arguments were pass in on the command line.  In this case it would be
     *the executable file, the input file, and the output file for a total of e
     *if argc does not = 3 then you have a problem*/
    if ( argc != 3 ) {
        fprintf (stderr, "Need two files names\n");
        return 1;
    }

    /*--now we need to open the files and make sure they are not null.  If they are null then
     *this means you can't open the file or the file is empty.  
     *If the file is null print a message to stderr indicating you could not 
     *read the file or that you could not write to the file and return to 
     *stop the program.
     *--FILE *fopen(const char *filename, const char *mode)  This is the 
     *signature for fopen. The parameters are const char pointer and a 
     *char pointer for the operation you want to do with the open file or 
     *more precisely the access identifier. In this case "r" stands for 
     *read and "w" stands for write.
     */
    if ( (in = fopen (argv[1], "r"))  ==  NULL ) {
        fprintf (stderr, "Can't read %s.\n", argv[1]);
        return 2;
    }

    if ( (out = fopen (argv[2], "w")) == NULL ) {
        fprintf (stderr, "Can't write %s.\n", argv[2]);
        return 3;
    }

    /*Loop throught the file one character at a time and copy it from 
     *file to the other.  in to out This continues until the EOF character 
     *encountered*/
    while ( (c = getc (in)) != EOF )
        putc (c, out);
    
    

    printf ("File has been copied.\n");

    /*If yo open a file you must close it.*/
    fclose (in);
    fclose (out);

    return 0;
}
