21xrx.com
2024-03-29 21:59:28 Friday
登录
文章检索 我的文章 写文章
复制文件的C程序
2021-07-07 10:07:13 深夜i     --     --
C

复制文件的C程序:该程序复制文件,首先指定要复制的文件,然后输入目标文件的名称和扩展名。 我们将以“读取”模式打开要复制的文件,以“写入”模式打开目标文件。

C程序


#include <stdio.h>
#include <stdlib.h>


int main()
{
   char ch, source_file[20], target_file[20];
   FILE *source, *target;

   printf("Enter name of file to copy\n");
   gets(source_file);

   source = fopen(source_file, "r");

   if (source == NULL)
   {
      printf("Press any key to exit...\n");
      exit(EXIT_FAILURE);
   }

   printf("Enter name of target file\n");
   gets(target_file);

   target = fopen(target_file, "w");

   if (target == NULL)
   {
      fclose(source);
      printf("Press any key to exit...\n");
      exit(EXIT_FAILURE);
   }

   while ((ch = fgetc(source)) != EOF)
      fputc(ch, target);

   printf("File copied successfully.\n");

   fclose(source);
   fclose(target);

   return 0;
}

下载文件复制程序。

程序输出:

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复