zoukankan      html  css  js  c++  java
  • C++复制文件(使用WindowsAPI)

    bool TForm1::CopyFile2() {
        AnsiString srcPath = ExtractFilePath(Application->ExeName) + "Windows.pdf";
        AnsiString destPath = ExtractFilePath(Application->ExeName) + "dest.pdf";

        HANDLE hSrcFile, hDestFile;
        hSrcFile = CreateFile(srcPath.c_str(),     // open MYFILE.TXT
                  GENERIC_READ,              // open for reading
                  FILE_SHARE_READ,           // share for reading
                  NULL,                      // no security
                  OPEN_EXISTING,             // existing file only
                  FILE_ATTRIBUTE_NORMAL,     // normal file
                  NULL);                     // no attr. template
        if (hSrcFile == INVALID_HANDLE_VALUE)
            return false;

        hDestFile = CreateFile(destPath.c_str(),           // create MYFILE.TXT
                 GENERIC_WRITE ,                // open for writing
                 0,                            // do not share
                 NULL,                         // no security
                 CREATE_ALWAYS,                // overwrite existing
                 FILE_ATTRIBUTE_NORMAL,       // normal file
                 NULL);
        if (hDestFile == INVALID_HANDLE_VALUE)
            return false;
        char* buffer = new char[1024];

        ULONG lpNumberOfBytesRead, lpNumberOfBytesWritten;
        DWORD fileSize, offset = 0;
        fileSize = GetFileSize(hSrcFile, NULL);
        if (fileSize == INVALID_FILE_SIZE)
            return false;
        while (offset < fileSize) {
            BOOL bFlag = ReadFile(hSrcFile, buffer, 1024, &lpNumberOfBytesRead, NULL);
            if (!bFlag) {
                return false;
            }
            WriteFile(hDestFile, buffer, 1024, &lpNumberOfBytesWritten, NULL);
            offset += 1024;
        }
        CloseHandle(hSrcFile);
        CloseHandle(hDestFile);
        delete[] buffer;
        return true;
    }

  • 相关阅读:
    如何使用第三方webservice
    SQL零星技术点:SQL中转换money类型数值转换为字符串问题
    P2664 树上颜色统计 点分治 虚树 树上差分 树上莫队
    SPOJ 1825 经过不超过K个黑点的树上最长路径 点分治
    P4149 距离为K的点对(最少边数) n=200000 点分治
    P2634 树上路径长度为3的倍数的点对数 点分治
    P3806 离线多次询问 树上距离为K的点对是否存在 点分治
    POJ 1741 单次询问树上距离<=K的点对数 点分治
    BZOJ 1016 生成树计数
    BZOJ 1015 并查集&连通块
  • 原文地址:https://www.cnblogs.com/yuanxiaoping_21cn_com/p/1332701.html
Copyright © 2011-2022 走看看