/* * Simple program to copy file, character by character. * Uses command-line arguments to specify filenames. */ #include int main(int argc, char* argv[]) { /* * check that user supplied two arguments * by convention argv[0] is something that identifies the * program (often its filename -- e.g., a.out). */ if (argc != 3) { printf("usage %s infile outfile\n", argv[0]); return 1; } /* * open input, output files -- note that fopen returns NULL if it * could not do this, e.g., input file not found. */ FILE * infile = fopen(argv[1], "r"); if (infile == NULL) { printf("could not open input file %s\n", argv[1]); return 1; } FILE * outfile = fopen(argv[2], "w"); if (outfile == NULL) { printf("could not open output file %s\n", argv[2]); return 1; } /* copy infile to outfile -- compare to copy.c */ int inchar; while ((inchar = fgetc(infile)) != EOF) { fputc(inchar, outfile); } /* * close files -- free up system resources, make sure all of * output file is actually written to disk */ fclose(infile); fclose(outfile); return 0; }