c++文件复制粘贴

#include
#include
using namespace std;
bool CopyFile(const char *src, const char *des)
{
    FILE * fSrc = fopen(src, "rb");
    if(!fSrc)
    {
        cout<<"打开文件"<         return false;
    }
    FILE * fDes = fopen(des, "wb");
    if(!fDes)
    {
        cout<<"创建文件"<         return false;
    }    
unsigned char * buf;
    unsigned int length;
    fseek(fSrc, 0, SEEK_END);
    length = ftell(fSrc);
    buf = new unsigned char[length+1];
    memset(buf, 0, length+1);
    fseek(fSrc, 0, SEEK_SET);
    fread(buf, length, 1, fSrc);    fwrite(buf, length, 1, fDes);    fclose(fSrc);
    fclose(fDes);
    delete [] buf;
    return true;
}
int main()
{
    char src[260] = { 0 };
    char des[260] = { 0 };
    cout<<"输入原文件名:";
    cin>>src;
    cout<<"输入目标文件名:";
    cin>>des;
    if(CopyFile(src, des))
        cout<<"文件复制成功"<     else
        cout<<"文件复制失败"<     return 0;
}

你可能感兴趣的:(c++)