/* * Version of countchars.c that reads and writes files. * * Filenames are specified as command-line arguments. */ #include #include int main(int argc, char* argv[]) { if (argc < 3) { fprintf(stderr, "usage %s infile outfile\n", argv[0]); return 1; } FILE * infile = fopen(argv[1], "r"); if (infile == NULL) { fprintf(stderr, "infile could not be opened\n"); return 1; } FILE * outfile = fopen(argv[2], "w"); if (outfile == NULL) { fprintf(stderr, "outfile could not be opened\n"); return 1; } int inchar; int count = 0; while ((inchar = fgetc(infile)) != EOF) { count += 1; fputc(toupper(inchar), outfile); } fclose(infile); fclose(outfile); printf("%d chars\n", count); return 0; }