scopy.c

/*
 * the simplest copy ever for text files
 * Usage: scopy fromfile tofile
 * 
 * Matt Bishop, ECS 36A, Fall 2019
 *
 * October 24, 2019
 */
#include <stdio.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 */

	/*
	 * 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(fgets(buf, BUFFERSIZE, infp) != NULL)
		if (fputs(buf, outfp) == EOF){
			fprintf(stderr, "%s: write error; quitting\n", argv[2]);
			return(1);
		}

	/* all done -- close everything up and exit */
	(void) fclose(infp);
	(void) fclose(outfp);
	return(0);
}


UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: [email protected]
ECS 36A, Programming & Problem Solving
Version of April 2, 2024 at 12:13PM

You can get the raw source code here.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh