/* ECP: FILEname=fig12_13.c */ /* 1*/ #include /* 2*/ #include /* 3*/ /* Copy From Sourcefile To Destfile And return The Number */ /* 4*/ /* Of Characters Copied; -1 For Errors */ /* 5*/ int /* 6*/ Copy( const char *DestFile, const char *SourceFile ) /* 7*/ { /* 8*/ int CharsCounted = 0; /* 9*/ int Ch; /*10*/ FILE *Sfp, *Dfp; /*11*/ if( strcmp( SourceFile, DestFile ) == 0 ) /*12*/ { /*13*/ printf( "Can not copy to self\n" ); /*14*/ return -1; /*15*/ } /*16*/ if( ( Sfp = fopen( SourceFile, "r" ) ) == NULL ) /*17*/ { /*18*/ printf( "Can not open input file %s\n", SourceFile ); /*19*/ return -1; /*20*/ } /*21*/ if( ( Dfp = fopen( DestFile, "w" ) ) == NULL ) /*22*/ { /*23*/ printf( "Can not open output file %s\n", DestFile ); /*24*/ fclose( Sfp ); return -1; /*25*/ } /*26*/ while( ( Ch = getc( Sfp ) ) != EOF ) /*27*/ if( putc( Ch, Dfp ) == EOF ) /*28*/ { /*29*/ printf( "Unexpected error during write.\n" ); /*30*/ break; /*31*/ } /*32*/ else /*33*/ CharsCounted++; /*34*/ fclose( Sfp ); /*35*/ fclose( Dfp ); /*36*/ return CharsCounted; /*37*/ }