1#include <iostream>2#include <fstream>3 4 voidCopyChar* SRC,Char*DST);5 intMain ()6 {7 using namespacestd;8 Charsrc[ -] ="E:/test/jdk-8u121-windows-x64.exe";9 Chardst[ -] ="E:\\test\\jdk-8u121-windows-x64_bak.exe";Ten copy (SRC, DST); One A return 0; - } - the voidCopyChar* SRC,Char*DST) - { - using namespacestd; -Ifstreaminch(src,ios::binary); +Ofstream out(dst,ios::binary); - if(!inch. Is_open ()) { +cout <<"Error Open File"<< src <<Endl; A exit (exit_failure); at } - if(! out. Is_open ()) { -cout <<"Error Open File"<< DST <<Endl; - exit (exit_failure); - } - if(src = =DST) { incout <<"The src file can ' t be same with DST file"<<Endl; - exit (exit_failure); to } + Charbuf[2048]; - Long LongTotalBytes =0; the while(inch) * { $ //read reads 2048 bytes from the in stream into the BUF array while the file pointer moves backwards by 2048 bytesPanax Notoginseng //if less than 2048 bytes are encountered at the end of the file, the actual fetch bytes are read. - inch. Read (BUF,2048); the //Gcount () is used to extract the number of bytes read, and write writes the contents of the BUF to the out stream. + out. Write (BUF,inch. Gcount ()); ATotalBytes + =inch. Gcount (); the } + inch. Close (); - out. Close (); $}
C + + File replication