鱼C论坛

 找回密码
 立即注册
查看: 3183|回复: 5

关于文件复制的程序

[复制链接]
发表于 2011-11-8 14:25:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
程序没错,但是我运行后,就出现 输入形式应为:COPYFILE 源文件名 目标文件名。  我无法输入源文件名和目标文件名,也就是说无法复制文件啊, 请问这该如何是好啊。 还有那个  exit(EXIT_FAILURE ) 可以写成 return 或者exit(0)吗??
#include<stdio.h>
#include<stdlib.h>

  int main(int argc, char*argv[])
  {
    FILE *in, *out;
    int ch;

        if(argc!=3)
        {
            fprintf(stderr, "输入形式应为:COPYFILE 源文件名 目标文件名\n");
            exit(EXIT_FAILURE );
        }
     if((in=fopen(argv[1],"rb"))==NULL)
         {
                fprintf(stderr, "该%s文件不能打开",argv[1]);
            exit(EXIT_FAILURE );
         }
     if((out=fopen(argv[2],"wb"))==NULL)
         {
          fprintf(stderr," 该%s文件不能打开",argv[2]);
           fclose(out);
          exit(EXIT_FAILURE );
         }

          while(ch=getc(in)!=EOF)
          {
                  if((putc(ch,out))==EOF)
                  {
                          break;
                  }
          }

         if(ferror(in))
          {
         printf("读取%s文件失败!",argv[1]);  
          }
        
          if(ferror(out))
          {
          printf("读取%s文件失败!",argv[2]);
          }
            printf("成功复制一个文件\n");
            fclose(in);
            fclose(out);
            return 0;
            system("pause");
  }
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2011-11-8 14:53:58 | 显示全部楼层
我初步估计这个代码不是你写的,
先弄好main函数的内两个参数是怎么用的再说吧!!!!
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2011-11-8 16:58:08 | 显示全部楼层
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. int main()
  4. {
  5.     FILE *in, *out;
  6.     int ch;

  7.         //if((in=fopen(argv[1],"rb"))==NULL)
  8.         if((in=fopen("C:\\data.txt","rb"))==NULL)
  9.         {
  10.         //        fprintf(stderr, "该%s文件不能打开",argv[1]);
  11.                 exit(EXIT_FAILURE );
  12.         }
  13.         //if((out=fopen(argv[2],"wb"))==NULL)
  14.         if((out=fopen("C:\\data_dup.txt","wb"))==NULL)
  15.         {
  16.         //        fprintf(stderr," 该%s文件不能打开",argv[2]);
  17.                 fclose(out);
  18.                 exit(EXIT_FAILURE );
  19.         }
  20.        
  21.         //while(ch=getc(in)!=EOF)
  22.         while((ch=getc(in))!=EOF)
  23.         {
  24.                 if((putc(ch,out))==EOF)
  25.                 {
  26.                         break;
  27.                 }
  28.         }
  29.         printf("成功复制一个文件\n");
  30.         fclose(in);
  31.         fclose(out);
  32.         return 0;
  33.         system("pause");
  34. }
复制代码
建立文件C:\data.txt,随便输入一些内容,运行本程序,会将C:\data.txt复制到C:\data_dup.txt
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2011-11-8 17:34:26 | 显示全部楼层

这个是小甲鱼是的c++快速人门视频里,然后我自己写了一遍。
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2011-11-8 17:59:20 | 显示全部楼层
仰望天上的光 发表于 2011-11-8 16:58
建立文件C:\data.txt,随便输入一些内容,运行本程序,会将C:\data.txt复制到C:\data_dup.txt

恩,我这是sudden insight , 我把这个程序编译好生成的copy.exe文件 复制到F盘  在 DOS 下运行  F:\ copy.exe  1.txt  2.txt 就 OK 了。谢谢你的提醒啊,小弟我感激不尽啊!   
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2011-11-8 18:24:47 | 显示全部楼层
是吗?至少
while(ch=getc(in)!=EOF)
要改为:
while((ch=getc(in))!=EOF)
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-9 09:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表