谁帮我看看这段,分割和合并二进制文件哪里出错了
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <string.h>
#include <malloc.h>
#include <memory.h>
char path[1024] = "C:\\Users\\Administrator\\Desktop\\X.PNG";
char **ppf = NULL;
int open_source_file()//打开源文件
{
FILE *pf = fopen(path, "rb");
if (pf == NULL)
{
puts("文件打开失败\n");
return -1;
}
else
{
fseek(pf, 0, SEEK_END);
int size = ftell(pf);
fclose(pf);
return size;
}
}
void subsectionjia(int sum)//分段加密
{
FILE *pfr = NULL;
FILE *pfw = NULL;
if ((pfr = fopen(path, "rb")) == NULL)
{
printf("文件打开失败\n");
return;
}
long long length = open_source_file();//获取源文件总字
if(length%sum == 0)
{
for (int i = 0; i < sum; i++)
{
pfw = fopen(ppf[i], "wb");
for (int j = 0; j < length / sum; j++)
{
fputc(fgetc(pfr), pfw);
}
fclose(pfw);
}
}
else
{
for (int i = 0; i < sum - 1; i++)
{
pfw = fopen(ppf[i], "wb");
for (int j = 0; j < length / (sum - 1); j++)
{
fputc(fgetc(pfr), pfw);
}
fclose(pfw);
}
{
pfw = fopen(ppf[sum - 1], "wb");
for (int i = 0; i < length%sum; i++)
{
fputc(fgetc(pfr), pfw);
}
fclose(pfw);
}
}
fclose(pfr);
}
void subsectionjie(int sum)
{
FILE *pnew = fopen("C:\\Users\\Administrator\\Desktop\\TT.PNG", "wb");
for (int i = 0; i <sum; i++)
{
FILE *p = fopen(ppf[i], "rb");
while (!feof(p))
{
fputc(fgetc(p), pnew);
}
fclose(p);
}
fclose(pnew);
}
void main()
{
int sum = 0;
scanf("%d", &sum);
ppf = malloc(sizeof(char *)*sum);
for (int i = 0; i < sum; i++)
{
ppf[i] = malloc(sizeof(char) * 1024);
memset(ppf[i], '\0', 1024);
sprintf(ppf[i], "C:\\Users\\Administrator\\Desktop\\X %d.PNG",i);
}
subsectionjia(sum);
subsectionjie(sum);
system("pause");
}
不知道 错误在哪!
分割4个 结果是这样的:
合并的结果 :
原始图片: