/* * the simplest copy ever (does binary too) * Usage: scopy fromfile tofile * * Matt Bishop, ECS 36A, Fall 2019 * * October 24, 2019 */ #include /* * macros */ #define BUFFERSIZE 1024 /* * the main routine */ int main(int argc, char *argv[]) { FILE *infp, *outfp; /* input, output file pointers */ char buf[BUFFERSIZE]; /* input buffer */ int nread; /* number of bytes read */ /* * open the files */ /* be sure there are 2 file names! */ if (argc != 3){ fprintf(stderr, "Usage: scopy fromfile tofile\n"); return(1); } /* open the input and output files and give */ /* an error message and exit on failure */ if ((infp = fopen(argv[1], "r")) == NULL){ fprintf(stderr, "%s: could not open\n", argv[1]); return(1); } if ((outfp = fopen(argv[2], "w")) == NULL){ fprintf(stderr, "%s: could not open\n", argv[2]); return(1); } /* * now copy from the source file to the destination file */ while((nread = fread(buf, sizeof(char), BUFFERSIZE, infp)) != 0) if (fwrite(buf, sizeof(char), nread, outfp) != nread){ fprintf(stderr, "%s: write error; quitting\n", argv[2]); return(1); } if (!feof(infp)){ fprintf(stderr, "%s: read error; quitting\n", argv[1]); return(1); } /* all done -- close everything up and exit */ (void) fclose(infp); (void) fclose(outfp); return(0); }