/** * cp.c * * week 6 section * fall 2013 * * copy a file */ #include #include typedef uint8_t BYTE; int main(int argc, char* argv[]) { // two and only two command-line arguments if (argc != 3) { printf("Usage: ./cp src dst\n"); return 1; } // remember filenames char* src = argv[1]; char* dst = argv[2]; // open input file FILE* srcptr = fopen(src, "r"); if (srcptr == NULL) { printf("Could not open %s.\n", src); return 2; } // open output file FILE* dstptr = fopen(dst, "w"); if (dstptr == NULL) { fclose(srcptr); fprintf(stderr, "Could not create %s.\n", dst); return 3; } // copy src into dest a byte at a time BYTE byte; while (fread(&byte, sizeof(BYTE), 1, srcptr)) { fwrite(&byte, sizeof(BYTE), 1, dstptr); } // close infile fclose(srcptr); // close outfile fclose(dstptr); // that's all folks return 0; }