为什么写文件报错write(fd1,buf,count) ,errno 是9 EBADF,代码如下

#include

 1<sys types.h="">   
 2#include<sys stat.h="">   
 3#include<fcntl.h>   
 4#include<unistd.h>   
 5#include<errno.h>   
 6#include<stdio.h>   
 7int main(int argc, char *argv[]){   
 8int fd,fd1,count;   
 9ssize_t ss_size;   
10char buf[1024];   
11char *sourcefile,*destfile;   
12char c_writebuf[1024];   
13if (argc != 3)   
14{   
15printf("Usage: run<comand> [two parameter]");   
16return -1;   
17}   
18sourcefile = argv[1];   
19destfile = argv[2];   
20printf("source file is %s\n",sourcefile);   
21printf("dest file is %s\n",destfile);   
22if((fd1 = open(destfile,O_CREAT,S_IRWXU)) == -1){   
23printf("Open Error\n Error No= %d\n",errno);   
24return -1;   
25}   
26if ((fd = open(sourcefile,O_RDONLY)) == -1){   
27printf("Error can't open the source file %s",sourcefile);   
28return -1;   
29}   
30printf("size of buf is %d\n",sizeof(buf));   
31while (( count = read(fd,buf,sizeof(buf)))&gt;0)   
32{   
33if (write(fd1,buf,count) != count) //write 后出错   
34printf("error No=%d\n",errno);   
35printf("write data error\n");   
36}   
37if (count == -1)   
38printf("read data error");   
39else   
40{   
41printf("Success\n");   
42}   
43if (close(fd) == -1 ){   
44printf("Error,Can't close the sourcefile");   
45return -1;   
46}   
47if (close(fd1) == -1 ){   
48printf("Error,Can't close the destfile");   
49return -1;   
50}   
51printf("Success\n");   
52return 0;   
53}   
54  
55\---------------------------------------------------------------   
56  
57if((fd1 = open(destfile,O_CREAT,S_IRWXU)) == -1){   
58^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^   
59没有指定打开模式,改为   
60if((fd1 = open(destfile,O_CREAT &amp;brvbarO_RDWR,S_IRWXU)) &lt;0){</comand></stdio.h></errno.h></unistd.h></fcntl.h></sys></sys>
Published At
Categories with 服务器类
Tagged with
comments powered by Disqus