一个很简单的问题 关于文件打开出错
源码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#define  BUF 1024
int main(int argc,char **argv)
{
FILE *from_fd,*to_fd;
   if(argc!=3)
   {
   printf("argument error");
   exit(1);
    }
    if((from_fd=fopen(argv[1],"w+b"))==NULL)
     {
       printf("open %s failure\n",argv[1]);
       exit(1);
     }
   if((to_fd=fopen(argv[2],"w+b"))==NULL);
     {   
     printf("OPEN FILE %s ERROR\n",argv[2]);
     exit(1);
     }
}
运行时:
qust@qust-K42JZ:~/test$ gcc fopen_file.c -o fopen_file
qust@qust-K42JZ:~/test$ ./fopen_file test.c newfile
OPEN FILE newfile ERROR
test.c newfile这两个文件都是不存在的,创建这两个文件为什么会出错呢??
但实际在目录下创建了这两个文件。望大家指教,谢谢!
------解决方案--------------------
if((to_fd=fopen(argv[2],"w+b"))==NULL);
去掉后面的分号
------解决方案--------------------楼上正解,因为无论如何,你都会走下面的
printf("OPEN FILE %s ERROR\n",argv[2]);
 exit(1);