#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define PATH "C:\\Users\\michael\\Desktop\\切割合并\\PIS.exe"
#define PATH_MERGE "C:\\Users\\michael\\Desktop\\切割合并\\PIS_merge.exe"
int getFileSize(char* path)
{
int size = -1;
FILE *pFile;
pFile = fopen(path, "rb");
if (NULL == pFile)
{
return size;
}
else
{
fseek(pFile, 0, SEEK_END);
size = ftell(pFile);
fclose(pFile);
pFile = NULL;
}
return size;
}
char ** init(int num)
{
char** path;
path = (char**)malloc(sizeof(char*)*num);
for (int i = 0; i < num; i++)
{
path[i] = (char*)malloc(sizeof(char)* 1024);
sprintf(path[i], "C:\\Users\\michael\\Desktop\\切割合并\\PIS_0%d.exe", i + 1);
printf("%s\n", path[i]);
}
return path;
}
void merge_action(char* file,int num)
{
char ** path = init(num);
FILE* pWrite = fopen(file, "wb");
int size = 0;
if (pWrite == NULL)
{
return;
}
for (int i = 0; i < num; i++)
{
size = getFileSize(path[i]);
if (size < 0)
{
return;
}
else
{
FILE *p = fopen(path[i], "rb");
for (int j = 0; j < size; j++)
{
fputc(fgetc(p), pWrite);
}
fclose(p);
}
}
fclose(pWrite);
}
void split_action(char * file, int num)
{
char ** path = init(num);
int size = -1;
FILE *pFile;
pFile = fopen(file, "rb");
if (NULL == pFile)
{
return ;
}
else
{
size = getFileSize(file);
if (size%num == 0)
{
for (int i = 0; i < num; i++)
{
FILE* pWrite = fopen(path[i], "wb");
for (int j = 0; j < size / num; j++)
{
fputc(fgetc(pFile), pWrite);
}
fclose(pWrite);
}
}
else
{
for (int i = 0; i < num - 1; i++)
{
FILE* pWrite = fopen(path[i], "wb");
for (int j = 0; j < size / (num - 1); j++)
{
fputc(fgetc(pFile), pWrite);
}
fclose(pWrite);
}
for (int k = 0; k < size % (num - 1); k++)
{
FILE* pWrite = fopen(path[num-1], "wb");
fputc(fgetc(pFile), pWrite);
fclose(pWrite);
}
}
}
fclose(pFile);
}
void main()
{
int num;
scanf("%d", &num);
split_action(PATH,num);
merge_action(PATH_MERGE, num);
system("pause");
}