CopyFile

/******************************************************************************************************
 * @file name:		  : 文本拷贝.c
 * @brief  		      :拷贝文件
 * @author 		      :wvjnuhhail@126.com
 * @date 			      :2024/07/11
 * @version 1.0 	  :V1.0
 * @property 		    :暂无
 * @note   		      :None
 * CopyRight (c)  2023-2024   wvjnuhhail@126.com   All Right Reseverd
 *******************************************************************************************************/

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#define BLOCK_NUM 5
#define BLOCK_SIZE 50
/*******************************************************************************************************
 * @funname         :	File_Copy
 * @brief           : 拷贝文件
 * @param           :
 *                  @a:obj_file_path
 *                  @b:new_file_path
 * @retval          : bool
 * @date 			      :2024/07/10
 * @version         :V1.0
 * @note   		      :None
 *******************************************************************************************************/
bool File_Copy(const char *obj_file_path, const char *new_file_path);
bool File_Copy(const char *obj_file_path, const char *new_file_path)
{
  FILE *obj_file_fp = fopen(obj_file_path, "r");
  FILE *new_file_fp = fopen(new_file_path, "w");

  if (obj_file_fp == (FILE *)NULL || new_file_fp == (FILE *)NULL)
  {
    perror("fopen ...");
    return false;
  }

  char file_data_buf[BLOCK_NUM * BLOCK_SIZE];

  int copy_data_size = 0;
  int offset_read_befor = 0;
  int offset_read_after = 0;
  while (1)
  {
    memset(file_data_buf, 0, BLOCK_NUM * BLOCK_SIZE);

    // 先读取数据
    offset_read_befor = ftell(obj_file_fp);
    if (fread(file_data_buf, BLOCK_SIZE, BLOCK_NUM, obj_file_fp) < BLOCK_NUM)
    {
      if (ferror(obj_file_fp))
      {
        perror("fread ...");
        return false;
      }

      if (feof(obj_file_fp))
      {
        offset_read_after = ftell(obj_file_fp);
        // 补写最后一次不满足块的个数的数据
        if (fwrite(file_data_buf, offset_read_after - offset_read_befor, 1, new_file_fp) < 1)
        {
          perror("fwrite ...");
          return false;
        }

        copy_data_size += (offset_read_after - offset_read_befor);
        printf("文本拷贝成功,总共拷贝【%d】字节!\n", copy_data_size);
        break;
      }
    }
    else // 再把数据写到新文件中
    {
      if (fwrite(file_data_buf, BLOCK_SIZE, BLOCK_NUM, new_file_fp) < BLOCK_NUM)
      {
        perror("fwrite ...");
        return false;
      }

      copy_data_size += BLOCK_SIZE * BLOCK_NUM;
    }
  }

  if (fclose(obj_file_fp) == -1)
  {
    perror("fclose ...");
    return false;
  }

  if (fclose(new_file_fp) == -1)
  {
    perror("fclose ...");
    return false;
  }

  return true;
}

int main(int argc, char *argv[]) // ./a.out 要拷贝的文件   新文件
{
  if (argc != 3)
  {
    printf("参数有误!\n");
    return -1;
  }

  if (File_Copy(argv[1], argv[2]) == false)
  {
    printf("文件拷贝失败!\n");
    return -1;
  }

  return 0;
}

image

posted @ 2024-07-11 15:45  WJnuHhail  阅读(1)  评论(0编辑  收藏  举报