/*
 * Version of echo-text.c that copies from input file to output file,
 * counting characters.
 *
 * Filenames are specified with command-line arguments.
 */
#include <stdio.h>

int main(int argc, char *argv[]) {
	if (argc != 3) { 
		printf("usage:  %s infile outfile\n", argv[0]);
		return 1;
	}
	FILE * instream = fopen(argv[1], "r");
	if (instream == NULL) { 
		printf("cannot open file %s\n", argv[1]);
		return 1;
	}
	FILE * outstream = fopen(argv[2], "w");
	if (outstream == NULL) { 
		printf("cannot open file %s\n", argv[2]);
		return 1;
	}

	int ch;
        int count = 0;
	while ((ch = fgetc(instream)) != EOF) {
		fputc(ch, outstream);
		count += 1;
	}
	fprintf(stderr, "%d characters copied\n", count);
	fclose(instream);
	fclose(outstream);
	return 0;
}