Sign-up....

关于文件的分割和合并问题纯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);

}

[3464 byte] By [msdn] at [2007-8-14 20:08:07]
# 1 Re: 关于文件的分割和合并问题纯C语言

if((out=fopen(p,"wb"))==null)

{

printf("cannot open file\n");

exit(1);

}

建议用out=fopen(p,"wb+"))==null

读写数据用fread和fwrite比较好....也比较快

van_ni at 2005-6-6 12:17:45 >
# 2 Re: 关于文件的分割和合并问题纯C语言

上面的改了该错误同样存在.......................

fufangpeng at 2005-6-6 12:21:53 >
# 3 Re: 关于文件的分割和合并问题纯C语言

char *p="\0";

printf("input the information file name:");

scanf("%s",p);

----

这里p好像没有分配内存?

zhousqy at 2005-6-6 12:36:11 >
# 4 Re: 关于文件的分割和合并问题纯C语言

没有人知道是什么问题吗?知道的说下啊??????????期待

fufangpeng at 2005-6-6 13:58:23 >
# 5 Re: 关于文件的分割和合并问题纯C语言

楼上的都说得差不多了,我就不去细看了。。顶。。

mostideal at 2005-6-6 19:23:00 >

C/C++

All Classified