C语言的疑惑
程序代码:
下面是关于做一个简单copy的代码,将参数一文件的内容复制到参数二文件上,代码一能够成功做实现,但代码二就不可以,参数一的内容怎么都复制不了到参数二上,不知哪里出了问题,能否给小弟指出,谢谢 代码一: #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <errno.h> #include <string.h> char *myname; int main(int argc, char **argv) { int fda, fdd, wcount, rcount; char *wfile,*rfile; char buffer[BUFSIZ]; myname = argv[0]; if( argc < 3 ) { fprintf(stderr, "%s: not enough parameters \n",myname); } rfile = argv[1]; wfile = argv[2]; fda = open(rfile, O_RDONLY); rcount = read(fda, buffer, sizeof(buffer)); fdd = open(wfile, O_RDWR|O_CREAT, 0666); wcount = write(fdd, buffer,rcount); if (wcount != rcount) { fprintf(stderr, "%s, %s: write err :%s\n", myname, wfile, strerror(errno)); } (void)close(fda); (void)close(fdd); return (0); } 代码二: #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <errno.h> #include <string.h> char *myname; int main(int argc, char **argv) { int fda, fdd, wcount, rcount; char *wfile,*rfile; char buffer[BUFSIZ]; if( argc < 3 ) { fprintf(stderr, "%s: not enough parameters \n",myname); } myname = argv[0]; rfile = argv[1]; wfile = argv[2]; if(fda = open(rfile, O_RDONLY) < 0) { fprintf(stderr, "%s: %s: cannot open for reading:%s\n", myname, rfile, strerror(errno)); return 1; } rcount = read(fda, buffer, sizeof(buffer)); if(fdd = open(wfile, O_RDWR|O_CREAT, 0666) < 0) { fprintf(stderr, "%s, %s: open fiel err :%s\n", myname, wfile,strerror(errno)); return 1; } wcount = write(fdd, buffer,rcount); if (wcount != rcount) { fprintf(stderr, "%s, %s: write err :%s\n", myname, wfile, strerror(errno)); } (void)close(fda); (void)close(fdd); return (0); }