c语言文件输入输出为什么不对

2025-04-08 12:42:50
推荐回答(2个)
回答1:

#include
#include
using namespace std;

int main(int argc,char **argv){
    FILE *in,*out;
    if((out=fopen(argv[1],"r"))==NULL)
    {
        cout<<"文件不存在"<        exit(0);
    }
    in=fopen(argv[2],"w+");
    char ch;
    ch=fgetc(out);
    while(ch!=EOF)
    {
        fputc(ch,in);
        ch=fgetc(out);
    }
    fclose(in);
    fclose(out);
    return 0;
}

改成我这样就可以了。

测试结果如图:

回答2:

#include
using namespace std;
int main(int argc,char **argv)
{ FILE *in,*out;
  char ch;
  if((in=fopen(argv[1],"r"))==NULL)
  { cout<<"文件不存在"<    exit(0);
  }
  if((out=fopen(argv[2],"r"))!=NULL)
  { cout<<"文件"<    ch=fgetc(stdin);
    if(ch=='Y'||ch=='y')
    { fclose(out);
      out=fopen(argv[2],"w");
    }
    else exit(0);
  }
  else out=fopen(argv[2],"w");
  while((ch=fgetc(in))!=EOF)
    fputc(ch,out);
  fclose(in);
  fclose(out);
  return 0;
}