如何比较两个文件内容是否相同
小弟想编程实现比较两个文件的内容是否相同,下面是代码:#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main()
{
int n1,n2,fd1,fd2,cr;
char *buf1[1024], *buf2[1024],*buf3[1024],*buf4[1024];
if((fd1=open("file1.c",O_RDONLY))==-1)
perror("open file1 error");
while((n1=read(fd1,buf1,1024))>0)
{
if(write(buf2,buf1,n1)!=n1)
perror("write error1");
}
if((fd2=open("file2.c",O_RDONLY))==-1)
perror("open file2 error");
while((n2=read(fd2,buf3,1024))>0)
{
if(write(buf4,buf3,n2)!=n2)
perror("write error2");
}
cr=strcmp(buf2,buf4);
if(cr==0)
printf("Two files are the same");
else
printf("Two files are different");
return 0;
}
编译执行后提示:
write error1: Bad file descriptor
write error2: Bad file descriptor
请高手指教下 什么问题啊?