/*
程序名:文件分割(合并)程序
作者:李腾
QQ:342736781
*/
#include <stdio.h>
#include <stdlib.h>
long filesize(FILE *fp){ //获取文件长度的函数
long fhead,fend,length;
fopen("fp","rb");
fhead=ftell(fp);
fseek(fp,0,SEEK_END);
fend=ftell(fp);
length=fend-fhead;
fseek(fp,0,SEEK_SET);
fclose(fp);
return length;
}
void partition(){
long length,a; //文件长度
char *inf,*out1,*out2,*buf; // 文件名
FILE *stream,*outp1,*outp2; //文件指针
inf=(char *)malloc(30);
out1=(char *)malloc(30); // 分配内存
out2=(char *)malloc(30);
putchar(10);
printf("输入被分割的文件名或绝对路径: ");
scanf("%s",inf);
stream=fopen(inf,"rb");
printf("文件已打开...");
length=filesize(stream);
printf("获取文件大小为:%ldByte.\n",length);
printf("请输入分割后的文件名1: ");
scanf("%s",out1);
printf("请输入分割后的文件名2: ");
scanf("%s",out2);
printf("正在分割文件...\n");
outp1=fopen(out1,"wb");
outp2=fopen(out2,"wb"); //打开分割后的文件
a=length/2;
buf=(char *)malloc(a+1);
fread(buf,a,1,stream);
fwrite(buf,a,1,outp1);
fread(buf,length-a,1,stream);
fwrite(buf,length-a,1,outp2);
fclose(stream);
fclose(outp1);
fclose(outp2);
free(inf);
free(out1);
free(out2);
free(buf);
printf("done\n");
system("pause");
}
void coalition(){
long length;
char *f1,*f2,*f3,*buf;
FILE *file1,*file2,*file3;
f1=(char *)malloc(30); //分配内存
f2=(char *)malloc(30);
f3=(char *)malloc(30);
putchar(10);
printf("请输入第一个要合并的文件路径: ");
scanf("%s",f1);
printf("请输入第二个要合并的文件路径: ");
scanf("%s",f2);
printf("请输入合并后的文件名: ");
scanf("%s",f3);
printf("正在合并文件...\n");
file1=fopen(f1,"rb");
file2=fopen(f2,"rb");
file3=fopen(f3,"wb");
length=filesize(file1);
buf=(char *)malloc(length);
fread(buf,length,1,file1);
fwrite(buf,length,1,file3);
length=filesize(file2);
buf=(char *)realloc(buf,length);
fread(buf,length,1,file2);
fwrite(buf,length,1,file3);
printf("done\n");
printf("被合并的文件必须在退出本程序以后才可使用.\n");
system("pause");
fclose(file1);
fclose(file2);
fclose(file2);
free(f1);
free(f2);
free(f3);
free(buf);
}
void main(){
char g;
for(;;){
printf("注意:文件名不可包含空格\n");
printf("1.分割文件\n");
printf("2.合并文件\n");
printf("3.退出\n");
printf("please input: ");
g=getch();
switch(g)
{
case '1':partition();break;
case '2':coalition();break;
case '3':exit(0);
default:printf("input error!\n");system("pause");
}
system("cls");
}
}
程序名:文件分割(合并)程序
作者:李腾
QQ:342736781
*/
#include <stdio.h>
#include <stdlib.h>
long filesize(FILE *fp){ //获取文件长度的函数
long fhead,fend,length;
fopen("fp","rb");
fhead=ftell(fp);
fseek(fp,0,SEEK_END);
fend=ftell(fp);
length=fend-fhead;
fseek(fp,0,SEEK_SET);
fclose(fp);
return length;
}
void partition(){
long length,a; //文件长度
char *inf,*out1,*out2,*buf; // 文件名
FILE *stream,*outp1,*outp2; //文件指针
inf=(char *)malloc(30);
out1=(char *)malloc(30); // 分配内存
out2=(char *)malloc(30);
putchar(10);
printf("输入被分割的文件名或绝对路径: ");
scanf("%s",inf);
stream=fopen(inf,"rb");
printf("文件已打开...");
length=filesize(stream);
printf("获取文件大小为:%ldByte.\n",length);
printf("请输入分割后的文件名1: ");
scanf("%s",out1);
printf("请输入分割后的文件名2: ");
scanf("%s",out2);
printf("正在分割文件...\n");
outp1=fopen(out1,"wb");
outp2=fopen(out2,"wb"); //打开分割后的文件
a=length/2;
buf=(char *)malloc(a+1);
fread(buf,a,1,stream);
fwrite(buf,a,1,outp1);
fread(buf,length-a,1,stream);
fwrite(buf,length-a,1,outp2);
fclose(stream);
fclose(outp1);
fclose(outp2);
free(inf);
free(out1);
free(out2);
free(buf);
printf("done\n");
system("pause");
}
void coalition(){
long length;
char *f1,*f2,*f3,*buf;
FILE *file1,*file2,*file3;
f1=(char *)malloc(30); //分配内存
f2=(char *)malloc(30);
f3=(char *)malloc(30);
putchar(10);
printf("请输入第一个要合并的文件路径: ");
scanf("%s",f1);
printf("请输入第二个要合并的文件路径: ");
scanf("%s",f2);
printf("请输入合并后的文件名: ");
scanf("%s",f3);
printf("正在合并文件...\n");
file1=fopen(f1,"rb");
file2=fopen(f2,"rb");
file3=fopen(f3,"wb");
length=filesize(file1);
buf=(char *)malloc(length);
fread(buf,length,1,file1);
fwrite(buf,length,1,file3);
length=filesize(file2);
buf=(char *)realloc(buf,length);
fread(buf,length,1,file2);
fwrite(buf,length,1,file3);
printf("done\n");
printf("被合并的文件必须在退出本程序以后才可使用.\n");
system("pause");
fclose(file1);
fclose(file2);
fclose(file2);
free(f1);
free(f2);
free(f3);
free(buf);
}
void main(){
char g;
for(;;){
printf("注意:文件名不可包含空格\n");
printf("1.分割文件\n");
printf("2.合并文件\n");
printf("3.退出\n");
printf("please input: ");
g=getch();
switch(g)
{
case '1':partition();break;
case '2':coalition();break;
case '3':exit(0);
default:printf("input error!\n");system("pause");
}
system("cls");
}
}