How to write 4 new versions of a file copy program?( c programming)
ID: 3666322 • Letter: H
Question
The program cp.c makes a copy of a file. It uses getc() and putc() to do the input and output. Write 4 new versions of a file copy program. Each of the programs should have the same functionality as cp.c.a)In 1st version, use fgetc() to do the input and fputc() to do the output.
b)in 2nd version,use fgets() to do input and fputs() to do the output.
c)in 3rd version,use fread() to do input and fwrite() to do output.
d)in 4th version,use fscanf() to do input and fprintf() to do output.
e)compare the sizes of the object code from all 5 versions of the file copy programs. If facilities of testing exection effeciency are available on your system, test the effeciency of each version and report your findings. The program cp.c makes a copy of a file. It uses getc() and putc() to do the input and output. Write 4 new versions of a file copy program. Each of the programs should have the same functionality as cp.c.
a)In 1st version, use fgetc() to do the input and fputc() to do the output.
b)in 2nd version,use fgets() to do input and fputs() to do the output.
c)in 3rd version,use fread() to do input and fwrite() to do output.
d)in 4th version,use fscanf() to do input and fprintf() to do output.
e)compare the sizes of the object code from all 5 versions of the file copy programs. If facilities of testing exection effeciency are available on your system, test the effeciency of each version and report your findings. Include Files/ include include main int arge, char *argv[] ) int main ( FILE*fpin, *fpout; int iochar; if argc3 printf( "Usage: cp oldfile newfileln" exit( 1) if (( fpin = fopen ( argv [1], "r" )) NULL ) { printf "Can't open input file.ln) exit( 1) if (( fpout = fopen( argv [2], "w" ))-NULL ) { printf "Can't open output file. In" i exit ( 1) while (( iochar = getc ( fpin )) != EOF ) putc( iochar, fpout): fclose( fpin)i fclose( fpout ) return 0
Explanation / Answer
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main( int argumntc, char * argumntv[] )
{
int commandCopy =( int ) argumntv[1][0] - '0' ;
if ( argumntc < 4 )
{
printf( " ERROR : Please type copy_command, source_filename 2 read from , & target_file 2 copy into . " ) ;
exit( 1 ) ;
}
printf( " CopyStarted_from : %s to %s " , argumntv[2] , argumntv[3] ) ;
switch( commandCopy )
{
case 1:
copy_v1_file( argumntv[2] , argumntv[3] ) ;
break;
case 2:
copy_v2_file( argumntv[2] , argumntv[3] ) ;
break;
case 3:
copy_v3_file( argumntv[2] , argumntv[3] ) ;
break;
case 4:
copy_v4_file( argumntv[2] , argumntv[3] ) ;
break;
default:
printf( " ERROR : Invalid command_number . " ) ;
exit( 1 ) ;
}
return 0 ;
}
// utilize fgetc () 2 do the input & fputc () 2 do the output.
void copy_v1_file( char copiedFrom[] , char copiedTo[] )
{
char letter ;
FILE * inputfile ;
FILE * outputfile ;
printf( " Using fgetc() for input & fputc() for output . " ) ;
inputfile =fopen( copiedFrom , "r" ) ;
if ( inputfile == NULL )
{
printf( " Error : Source_file doesn't have . " ) ;
exit( 1 ) ;
}
outputfile =fopen( copiedTo , "w" ) ;
while((letter =fgetc( inputfile ) ) != EOF )
{
fputc( letter , outputfile ) ;
}
fclose( inputfile ) ;
fclose( outputfile ) ;
printf( " Completed copying ... " ) ;
}
// utilize fgets () 2 do the input & fputs () 2 do the output .
void copy_v2_file( char copiedFrom[] , char copiedTo[] )
{
int maximun_lengths = 79 ;
char linesCount[maximun_lengths] ;
FILE * inputfile ;
FILE * outputfile ;
printf( " Using fgets() for input & fputs() for output . " ) ;
inputfile =fopen( copiedFrom , "r" ) ;
if ( inputfile == NULL )
{
printf( " Error : Source_file doesn't have . " ) ;
exit( 1 ) ;
}
outputfile =fopen( copiedTo , "w" ) ;
while( fgets ( linesCount , maximun_lengths + 1 , inputfile ) != NULL )
{
fputs( linesCount , outputfile ) ;
}
fclose( inputfile ) ;
fclose( outputfile ) ;
printf( " Compteted copying ... " ) ;
}
// utilize fread() 2 do the input & fwrite() 2 do the output .
void copy_v3_file( char copiedFrom[] , char copiedTo[] )
{
int file_maximun_size = 6400 , j=0 ;
char file_stream[file_maximun_size] ;
FILE * inputfile ;
FILE * outputfile ;
printf( " Using fread() for input & fwrite() for output . " ) ;
inputfile =fopen( copiedFrom , "r" ) ;
if( inputfile == NULL )
{
printf( " Error : Source_file doesn't have . " ) ;
exit( 1 ) ;
}
outputfile =fopen( copiedTo , "w" ) ;
fread( file_stream , sizeof(char) , file_maximun_size , inputfile ) ;
while( file_stream[j] != '' && j < file_maximun_size )
{
fwrite( &file_stream[j] , sizeof(char) , 1 , outputfile ) ;
j++ ;
}
fclose( inputfile ) ;
fclose( outputfile ) ;
printf( " Completed copying . " ) ;
}
// utilize fscanf() 2 do the input & fprintf() 2 do the output .
void copy_v4_file( char copiedFrom[] , char copiedTo[] )
{
FILE * inputfile ;
FILE * outputfile ;
printf( " Using fscanf() for input & fprintf() for output . " ) ;
inputfile =fopen( copiedFrom , "r" ) ;
if( inputfile == NULL )
{
printf( " Error : Source_file doesn't have . " ) ;
exit( 1 ) ;
}
outputfile =fopen( copiedTo , "w" ) ;
while( fscanf( inputfile , "" ) != EOF ) ;
{
fprintf( outputfile , "" ) ;
}
fclose( inputfile ) ;
fclose( outputfile ) ;
printf( " Completed copying ... " ) ;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.