C++复制文件的代码

bool TForm1::CopyFile() {
    DWORD fileSize, pos;
    int readLen;
    AnsiString srcPath = ExtractFilePath(Application->ExeName) + "Windows.pdf";
    AnsiString destPath = ExtractFilePath(Application->ExeName) + "dest.pdf";
    FILE *src, *dest;
    src = fopen(srcPath.c_str(), "r+b");

    if (src == NULL)
        return false;
    dest = fopen(destPath.c_str(), "ab");

    char *buffer = new char[1024];
    //char buffer[1024];
    //memset(buffer, 0, 1024);
    fseek(src, 0, 2);
    fileSize = ftell(src); //取得文件的大小
    fseek(src, 0, 0);
    do {
        readLen = fread(buffer, sizeof(char), 1024, src);
        if(readLen > 0) {
            pos += readLen;
            //对读取的文件做处理
            fwrite(buffer, sizeof(char), 1024, dest);
        }
        //memset(buffer, 0, 1024);
    } while (pos < fileSize); //循环读取文件
    delete[] buffer;
    fclose(src); //释放资源
    fclose(dest);
    return true;
}

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