/* * Simple contrived example of character I/O on files: * Copy characters from input to output, counting them. * Filenames given as command-line arguments. */ #include int main(int argc, char *argv[]) { if (argc != 3) { printf("arguments: infile outfile\n"); return 1; } FILE * infile = fopen(argv[1], "r"); if (infile == NULL) { printf("cannot open input\n"); return 1; } FILE * outfile = fopen(argv[2], "w"); if (outfile == NULL) { printf("cannot open output\n"); return 1; } /* code from simple-chars.c, reworked a bit */ int inchar; int count = 0; while ((inchar = fgetc(infile)) != EOF) { count += 1; fputc(inchar, outfile); } printf("%d chars\n", count); fclose(infile); fclose(outfile); return 0; }