下面是我写的一段程序,功能就是比较两本词典的,并把B词典中没有的词条从A中拷贝过来,可是有一点问题,请各位前辈帮我看看
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <malloc.h>
#include <windows.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <process.h>
#define
filename_1 "2.txt"
#define
filename_2 "3.txt"
int main()
{
FILE *fp1;
FILE *fp2;
struct stat statbuf,statbuf1;
char *p1,*p2;
char
buf1[1024]={0},buf3[1024]={0};
char
*buf2=NULL,*buf4=NULL;
char
buf5[1024]={0},buf6[1024]={0};
char *blank = " ";
stat(filename_1,&statbuf);
stat(filename_1,&statbuf1);
printf("filename_1 size:%ld\n filename_2 size:%ld\n",statbuf.st_size,statbuf1.st_size);
if ((fp1 = fopen(filename_1, "r")) == NULL)
{
fprintf(stderr, "Cannot open output filename_1.\n");
return 1;
}
if ((fp2 = fopen(filename_2, "rw")) == NULL)
{
fprintf(stderr, "Cannot open output filename_2.\n");
return 1;
}
buf2=(char*)malloc(statbuf.st_size+1);
buf4=(char*)malloc(statbuf1.st_size+1);
if (buf2 == NULL || buf4 == NULL)
{
printf("Not enough memory to allocate buffer\n");
exit(1);
/* terminate program if out of memory */
}
while((p1=fgets(buf2,statbuf.st_size,fp1)))///开始读取
{
if(*p1=='#')
{
strcpy(buf1,buf2);
printf("buf1:%s\n",buf1);
memset(buf3, 0, strlen(buf2));
strcat(buf3,buf1);
}
else
{
strcat(buf3,blank);
strcat(buf3,buf2);
printf("buf3:%s\n",buf3);
//
memset(buf1, 0, strlen(buf2));
}
while((p2=fgets(buf4,statbuf1.st_size,fp2)))
{
if(*p2=='#')
{
strcpy(buf5,buf4);
if(strcmp(buf1,buf5)==0)
{
break;
}
if(*p2==NULL)
{
fwrite(buf3,strlen(buf3),1,fp2);
//
想写到文件末尾,这里不知道怎么搞
memset(buf3, 0, strlen(buf2));
}
printf("buf5:%s\n",buf5);
memset(buf5, 0, strlen(buf4));
}
memset(buf4, 0, statbuf1.st_size);
}
memset(buf2, 0, statbuf.st_size);
}
fclose(fp1);
free(buf2);
fclose(fp2);
free(buf4);
return 0;
}