Saturday, November 28, 2009

study notes for fstream (2)


#include
#include
using namespace std;

int copy (const char* A, const char* B){
ifstream fin (A, ios::in | ios::binary);
ofstream fout (B, ios::out | ios:: binary);
if (!fin.is_open() || !fout.is_open()){
return -1;
}

char buf [512];
while (!fin.eof() && !fin.fail() && !fout.fail()){
fin.read(buf, 512);
int n = fin.gcount();
if ( n > 0 ){
fout.write(buf, n);
}
}

return fin.eof() ? 0 : -1;
}

int main(){

int ret = copy ("E:\\sxz\\1.docx" , "E:\\sxz\\B.docx");
cout << "ret=" << ret << endl;
return 0;
}

No comments:

Post a Comment