您的位置:首页 > 编程语言 > C语言/C++

C++ stl 复制文件的方法

2008-11-13 16:40 337 查看
头文件
#ifndef ioshelperH
#define ioshelperH

#include <fstream>
#include <vcl.h>
//---------------------------------------------------------------------------

class IOHelper
{
public:
void CopyFiles(char* srcPath, char* destPath);
};
#endif
======================================================
源码
void IOHelper::CopyFiles(char* srcPath, char* destPath)
{
//using namespace std;
ifstream src(srcPath, ios::binary | ios::in);
if (src.fail()) {
//"file src does not exist"
return;
}
ofstream dest(destPath, ios::out | ios::binary | ios::app);

char buffer[32767];

//获取文件大小
//int filesize;
//src.seekg(ios::beg, ios::end);
//filesize = src.tellg();

src.seekg(ios::beg);
dest.seekp(0);

while (src.good())
{
memset(buffer, 0, sizeof(char) * 32767);
src.read(buffer, 32767);

dest.write(buffer, src.gcount());

if (src.gcount() < 32767)
break;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: