/*
* the simplest copy ever (does binary too)
* Usage: scopy fromfile tofile
*
* Matt Bishop, ECS 36A, Fall 2019
*
* October 24, 2019
* -- original version
* May 1, 2024
* -- fixed some errors in types
*/
#include <stdio.h>
#include <sys/types.h>
/*
* 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 */
size_t 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);
}
|
ECS 36A, Programming & Problem Solving Version of April 2, 2024 at 12:13PM
|
You can get the raw source code here. |