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

  • 相关阅读:
    C#利用反射动态调用类及方法
    系统程序监控软件
    SQL server 2008 安装和远程访问的问题
    sql server 创建临时表
    IIS 时间问题
    windows 2008 安装 sql server 2008
    sql server xml nodes 的使用
    Window 7sp1 安装vs2010 sp1 打开xaml文件崩溃
    CSS资源网址
    Could not load type 'System.ServiceModel.Activation.HttpModule' from assembly 'System.ServiceModel, Version=3.0.0.0
  • 原文地址:https://www.cnblogs.com/yuanxiaoping_21cn_com/p/1332701.html
Copyright © 2011-2022 走看看