#include <stdio.h>

/* --------------------------------------------------------------------------
   Text copy program.
   Written by Arthit Hongchintakul

   Open a text file, print it line by line on a screen, and write it to another
   file. A practical example to file I/O. 

   After compiling:
   a.out input output
   Note:This  means we copy text from file called input to file called output
   -------------------------------------------------------------------------- */

/* Library function prototypes included in <stdio.h>
FILE * fopen(const char * filename, const char * mode);
int fclose(FILE * stream);
*/



int main(int argc, char * argv[])
{
  char * inputname;
  char * outputname;

  FILE * inputfile;
  FILE * outputfile;

  int charread = 0;
  if (argc != 3)
  {
    printf("Usage: textcopy inputfile outputfile.\n");
    return 1;
  }
  
  inputname = argv[1];
  outputname = argv[2];

  /* Open/ create files. */
  inputfile = fopen(inputname,"r");        /* mode "Read"  */
  outputfile = fopen(outputname,"w");     /* mode "Write" */
  
  if (inputfile == NULL || outputfile == NULL) /* files did not open */
  {
    printf("Files could not be opened\n");
    return 1; /* quit now */
  }

  while ((charread = fgetc(inputfile)) != EOF)
  {
    printf("%c", charread);
    fprintf(outputfile, "%c", charread);
  }
   
  /* close the file */
  if (fclose(inputfile) != 0 || fclose(outputfile) != 0) 
  {
    printf("Close file error.");
  }
  return 0;
}
