一个音频/视频太大,以下代码就能将其分割成多个模块,分模块传输,解决文件过大,传输不了的问题
分割文件
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
char path[100] = "C:\\Users\\Charging\\Desktop";
char filename[50] = "111.mp3";
char lastpath[150];
sprintf(lastpath, "%s\\%s", path, filename);
int filesize = 0;
FILE *fp = fopen(lastpath, "rb");
if (fp == NULL)
{
perror("合并失败的原因");
return;
}
fseek(fp, 0, SEEK_END);
filesize = ftell(fp);
printf("文件有%d个字节\n", filesize);
int setsize = 2 * 1024 * 1024;
int N;
if (filesize % setsize == 0)
{
N = filesize / setsize;
}
else
{
N = filesize / setsize + 1;
}
printf("N的个数是%d,setsize的大小是%d,filesize的大小是%d\n", N, setsize, filesize);
fseek(fp, 0, SEEK_SET);
char listpath[100] = "C:\\Users\\Charging\\Desktop\\list.txt";
FILE *plist = fopen(listpath, "w");
for (int i = 1;i <= N;i++)
{
char temppath[120];
sprintf(temppath, "%s\\%d%s", path, i, filename);
printf("%s\n", temppath);
fputs(temppath, plist);
fputc('\n', plist);
FILE *po = fopen(temppath, "wb");
if (i < N)
{
for (int j = 0; j < setsize; j++)
{
char ich = fgetc(fp);
fputc(ich, po);
}
}
else
{
for (int j = 0; j < filesize - (N - 1)*setsize; j++)
{
int ich = fgetc(fp);
fputc(ich, po);
}
}
fclose(po);
}
fclose(plist);
fclose(fp);
system("pause");
}
合并文件(基于分割完文件的基础上)
void main()
{
char path[100] = "C:\\Users\\Charging\\Desktop\\list.txt";
FILE *fp = fopen(path, "r");
char finapath[100] = "C:\\Users\\Charging\\Desktop\\合并昼颜.mp3";
FILE *fpfina = fopen(finapath, "wb");
char temppath[200];
while (fgets(temppath, 200, fp))
{
printf("%s", temppath);
int len = strlen(temppath);
temppath[len - 1] = '\0';
{
FILE *fpb = fopen(temppath, "rb");
int ich = fgetc(fpb);
while (!feof(fpb))
{
fputc(ich, fpfina);
ich = fgetc(fpb);
}
fclose(fpb);
}
}
fclose(fpfina);
fclose(fp);
system("pause");
}