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