文件分块器

今天来酷家乐云核计算公司面试实习生,面试题目是设计一种工具可以完成文件的分割和组合两种功能。代码如下,这个代码是网上找到的,自己修改一下能运行就准备交卷了。mark一下,回去后好好分析代码。

#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <commdlg.h>


int main(int argc, char* argv[])
{
    int num = atoi(argv[1]);
    OPENFILENAME File;
    static char filepath[MAX_PATH];
    LPTSTR filename = filepath;
    ZeroMemory(&File, sizeof(File));
    File.lStructSize = sizeof(File);
    File.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT;
    File.hwndOwner = NULL;
    File.lpstrFilter = "All Files (*.*)\0*.*\0";
    File.nFilterIndex = 1;
    File.lpstrFile = filepath;
    File.nMaxFile = sizeof(filepath);
    if (GetOpenFileName(&File))
    {
        LPTSTR SavePath = "D:\\fileClassifier";
        HANDLE rf = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
        if (rf != INVALID_HANDLE_VALUE)
        {
            DWORD FileLength = GetFileSize(rf, NULL);
            DWORD PartLength = FileLength / num + FileLength % num;
            int nCount = (FileLength + (FileLength%PartLength == 0 ? 0 : PartLength - FileLength%PartLength)) / PartLength;
            char* pBuf = new char[PartLength];
            int len = strlen(filename);
            int c;
            for (c = len; c > 0; c--)
                if (filename[c] == '\\')
                    break;
            filename += c + 1;
            for (int i = 1; i <= nCount; i++)
            {
                char temppath[MAX_PATH];
                char tempnum[2];
                DWORD ByteCount=0;
                ByteCount=(i < nCount ? ByteCount + PartLength : FileLength - PartLength);
                ReadFile(rf, pBuf, PartLength, &ByteCount, 0);
                strcpy_s(temppath, SavePath);
                strcat_s(temppath, "\\");
                strcat_s(temppath, filename);
                wsprintf(tempnum, "%d", i);
                strcat_s(temppath, tempnum);
                HANDLE wf = CreateFile(temppath, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
                DWORD BackBytes = i<nCount ? PartLength : FileLength - ByteCount;
                WriteFile(wf, pBuf, ByteCount, &BackBytes, 0);
                FlushFileBuffers(wf);
                CloseHandle(wf);
            }
            CloseHandle(rf);
            delete pBuf;
        }//分割文件
        LPTSTR savepath = SavePath;
        char tempfilepath[MAX_PATH];
        strcpy_s(tempfilepath, SavePath);
        strcat_s(tempfilepath, "\\");
        strcat_s(tempfilepath, filename);
        filename = tempfilepath;
        //strcpy_s(filename, tempfilepath);
        HANDLE writefile = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
        if (writefile != INVALID_HANDLE_VALUE)
        {
            int filecount = 1;
            char numchar[2];
            char temppath[MAX_PATH] = { 0 };
            strcpy_s(temppath, filename);
            wsprintf(numchar, "%d", filecount++);
            strcat_s(temppath, numchar);
            while (GetFileAttributes(temppath) != -1)
            {
                HANDLE readfile = CreateFile(temppath, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
                DWORD FileLength = GetFileSize(readfile, NULL);
                DWORD BuffSize = FileLength<10485760L ? FileLength : 10485760L;
                int nCount = (FileLength + (FileLength%BuffSize == 0 ? 0 : BuffSize - FileLength%BuffSize)) / BuffSize;
                char* pBuf = new char[BuffSize];
                for (int i = 1; i <= nCount; i++)
                {
                    DWORD ByteCount = (i<nCount ? ByteCount + BuffSize : FileLength - BuffSize);
                    ReadFile(readfile, pBuf, BuffSize, &ByteCount, 0);
                    DWORD BackBytes = i<nCount ? BuffSize : FileLength - ByteCount;
                    WriteFile(writefile, pBuf, ByteCount, &BackBytes, 0);
                }
                FlushFileBuffers(writefile);
                CloseHandle(readfile);
                delete pBuf;
                strcpy_s(temppath, filename);
                wsprintf(numchar, "%d", filecount++);
                strcat_s(temppath, numchar);
            }
            CloseHandle(writefile);
        }
    }//合成图像
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值