• 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;
    }

  • 相关阅读:
    echarts 立体图
    css 设置边框边角
    PS2020 快速设置文字渐变
    idea 2019 永久破解
    使用VUE+element ui 实现输入框 占位符自动补全功能
    纯css 设置隔行样式
    CSS 设置float:left 导致后面元素错乱问题
    c primer plus 4编程练习
    c语言中以八进制数表示字符、并输出
    c语言中printf()函数的返回值
  • 原文地址:https://www.cnblogs.com/yuanxiaoping_21cn_com/p/1332701.html
Copyright © 2020-2023  润新知