/* ECP: FILEname=fig14_3.cpp */ /* 1*/ #include /* 2*/ #include /* 3*/ #include /* 4*/ // Copy from sourcefile to destfile. Return number of /* 5*/ // Characters copied; -1 for errors. /* 6*/ int /* 7*/ Copy( const char *DestFile, const char *SourceFile ) /* 8*/ { /* 9*/ int CharsCounted = 0; /*10*/ char Ch; /*11*/ if( strcmp( SourceFile, DestFile ) == 0 ) /*12*/ { /*13*/ cerr << "Can not copy to self" << endl; /*14*/ return -1; /*15*/ } /*16*/ ifstream Sfp( SourceFile, ios::in ); /*17*/ if( !Sfp ) /*18*/ { /*19*/ cerr << "Can not open input file " << SourceFile << endl; /*20*/ return -1; /*21*/ } /*22*/ ofstream Dfp( DestFile, ios::out ); /*23*/ if( !Dfp ) /*24*/ { /*25*/ cerr << "Can not open output file " << DestFile << endl; /*26*/ return -1; /*27*/ } /*28*/ while( Sfp.get( Ch ) ) /*29*/ if( !Dfp.put( Ch ) ) /*30*/ { /*31*/ cerr << "Unexpected error during write." << endl; /*32*/ break; /*33*/ } /*34*/ else /*35*/ CharsCounted++; /*36*/ return CharsCounted; /*37*/ }