关于文件的分割和合并问题纯C语言
最近在TC下写了个关于文件分割和合并的程序,分割实现了,合并提示写错误,合并后的文件已生成.请高手指点,
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define null 0
#define blocksize 128/*分割后文件的大小*/
void partition()/*分割函数*/
{
FILE *in,*out;
char ch;
int temp;
char ch1;
int f_count = 1;
char *p = "\0";
printf("please input the file name split:");/*输入要分割的文件名,带路径*/
scanf("%s",p);
printf("\n");
if((in=fopen(p,"rb"))==null)
{
printf("connot open the file!");
exit(1);
}
printf("please input the small file name:");/*输入分割后的文件名,带路径*/
scanf("%s",p);
if((out=fopen(p,"wb"))==null)
{
printf("connot open file\n");
exit(1);
}
temp = blocksize;
while(!feof(in))
{
ch = getc(in);
if(ferror(in))
{
printf("read error\n");
exit(1);
}
else
{
putc(ch,out);
if(ferror(out))
{
printf("write error\n");
exit(1);
}
}
temp--;
if(0==temp)
{
fclose(out);
printf("input%dthe small file name:",f_count+1);/*输入第N个分割后的文件名*/
scanf("%s",p);
if((out=fopen(p,"wb"))==null)
{
printf("connot open file\n");
exit(1);
}
temp = blocksize;
f_count++;
}
}
fclose(out);
printf("input the information file name:");/*输入信息文件名*/
/*写信息文件*//*包括小文件的大小,小文件的个数*/
scanf("%s",p);
if((out=fopen(p,"wb"))==null)
{
printf("connot open file\n");
exit(1);
}
p = "the blocksize is ";
ch1 = blocksize ;
while(*p)
{
ch = *p;
putc(ch,out);
p++;
}
putc(ch1,out);
p = "there is:";
ch1 = f_count + '0';
while(*p)
{
ch = *p;
putc(ch,out);
p++;
}
putc(ch1,out);
p = "block";
while(*p)
{
ch = *p;
putc(ch,out);
p++;
}
fclose(in);
fclose(out);
}
void amalgamate()/*合并函数*/
{
FILE *in,*out;
int n =1;
char ch;
int temp;
int f_count = 0 ;
char *p="\0";
printf("input the information file name:");
scanf("%s",p);
if((in=fopen(p,"rb"))==null)
{
printf("connot open file\n");
exit(1);
}
while(getc(in)!=EOF)/*取得小文件的个数*/
{
ch = getc(in);
if(ch == ':')
{
f_count = getc(in) - '0';
break;
}
}
fclose(in);
printf("input the small file:");/*输入小文件名*/
scanf("%s",p);
printf("\n");
if((in=fopen(p,"rb"))==null)
{
printf("cannot open file\n");
exit(1);
}
printf("input the big file:");/*输入合并后的文件名*/
scanf("%s",p);
if((out=fopen(p,"wb"))==null)
{
printf("cannot open file\n");
exit(1);
}
temp = blocksize;
while(!feof(in))
{
if(f_count == 0)
break;
ch = getc(in);
if(ferror(in))
{
printf("read error\n");
exit(1);
}
else
{
putc(ch,out);
if(ferror(out))
{
printf("write error\n");
exit(1);/*每次都是执行到这里,我晕*//*小弟想了两天没有想明白*/
}
temp--;
if(temp==0)
{
fclose(in);
printf("input the %d file name:", n+1);
scanf("%s",p);
if((in=fopen(p,"rb"))==null)
{
printf("cannot open file\n");
exit(1);
}
temp = blocksize;
f_count--;
n++;
}
}
fclose(in);
fclose(out);
}
}
void main()
{
char c;
do
{
printf("1:Partition file\n2:Amalgamate files\n3:Quit\n");
c = getchar();
if(('p'==c)||('P'==c))
{
partition();
}
if(('A'==c)||('a'==c))
{
amalgamate();
}
if(('q'==c)||('Q'==c))
{
printf("this is quit!");
break;
}
}while(1);
}

