fstream 的用法——文件拷贝

转自 http://blog.sina.com.cn/s/blog_700a65cc0100mieb.html  
当ifstream read到文件尾,返回0值,其它时候返回非0值。下面给出一个用C++标准库实现文件拷贝的函数:
 
#include
#include
#include
using namespace std;
 
const int BUFSIZE = 1024 * 1024;
 
void  CopyRawFile(string InFile, string OutFile)
{
     char* pchar = newchar[BUFSIZE];
 
     ofstream ofile;
     ifstream ifile;
 
     ofile.open(OutFile.c_str(), ios::binary);
     ifile.open(InFile.c_str(), ios::binary);
 
     while(ifile.read(pchar, BUFSIZE))
         ofile.write(pchar, BUFSIZE);
 
     ofile.write(pchar, ifile.gcount());
     ifile.close();
     ofile.close();
     delete []pchar;
}

你可能感兴趣的:(编程)