为什么写文件报错write(fd1,buf,count) ,errno 是9 EBADF,代码如下
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<errno.h>
#include<stdio.h>
int main(int argc, char *argv[]){
int fd,fd1,count;
ssize_t ss_size;
char buf[1024];
char *sourcefile,*destfile;
char c_writebuf[1024];
if (argc != 3)
{
printf("Usage: run<comand> [two parameter]");
return -1;
}
sourcefile = argv[1];
destfile = argv[2];
printf("source file is %s\n",sourcefile);
printf("dest file is %s\n",destfile);
if((fd1 = open(destfile,O_CREAT,S_IRWXU)) == -1){
printf("Open Error\n Error No= %d\n",errno);
return -1;
}
if ((fd = open(sourcefile,O_RDONLY)) == -1){
printf("Error can't open the source file %s",sourcefile);
return -1;
}
printf("size of buf is %d\n",sizeof(buf));
while (( count = read(fd,buf,sizeof(buf)))>0)
{
if (write(fd1,buf,count) != count) //write 后出错
printf("error No=%d\n",errno);
printf("write data error\n");
}
if (count == -1)
printf("read data error");
else
{
printf("Success\n");
}
if (close(fd) == -1 ){
printf("Error,Can't close the sourcefile");
return -1;
}
if (close(fd1) == -1 ){
printf("Error,Can't close the destfile");
return -1;
}
printf("Success\n");
return 0;
}