C Programming

EXERCISE C27
Rewrite the program which copies files, ie, FCOPY.C to accept the source and destination filenames from the command line. Include a check on the number of arguments passed.


	#include <stdio.h>

	main( int argc, char *argv[])
	{
		FILE *in_file, *out_file, *fopen();
		int c;

		if( argc != 3 )
		{
			printf("Incorrect, format is FCOPY source dest\n");
			exit(2);
		}
		in_file = fopen( argv[1], "r");
		if( in_file == NULL )  printf("Cannot open %s for reading\n", argv[1]);
		else {
			out_file = fopen( argv[2], "w");
			if ( out_file == NULL )
				printf("Cannot open %s for writing\n", argv[2]);
			else {
				printf("File copy program, copying %s to %s\n", argv[1],  argv[2]);
				while ( (c=getc( in_file) ) != EOF )
					putc( c, out_file );
				putc( c, out_file);                  /* copy EOF */
				printf("File has been copied.\n");
				fclose( out_file);
			}
			fclose( in_file);
		}
	}


©Copyright B Brown. 1984-1999. All rights reserved.