利用fopen/fwrite手动实现文件拷贝copy功能

  vim fopen.c 在终端创建一个fopen.c文件并打开

  1 #include <stdio.h>

  2 #include <stdlib.h>
  3 int main(int argc,char *argv[])  //main函数原型
  4 {
  5     if(argc < 3)
  6     {
  7         printf("too few argment\n");
  8     }
  9     char *dstfile = argv[2];
 10     char *srcfile = argv[1];
 11     FILE *srcFile = fopen(srcfile,"r+");//r+:Create an empty file and open it for update (both for input and output). If a file with the same name already exists its contents are discarded and the file is treated as a new empty file.创建一个空文件
 12     //创建源文件指针
 13     if(srcFile == NULL)  //判断源文件是否存在
 14     {
 15         printf("srcFile open failed!\n");
 16         exit(-1);  //异常退出   exit(0)表示正常退出
 17     }
 18     //创建目的文件指针
 19     FILE *dstFile = fopen(dstfile,"w+");//w+表示写方式: Open a file for update (both for input and output). The file must exist. 打开一个文件,并且该文件必须存在
 20     if(dstFile == NULL)
 21     {
 22         printf("dstFile open failed!\n");
 23         exit(-1);
 24     }
 25     char buffer[10000];
 26     //创建一个数组,用于接收字符串
 27     for(;;)
 28     {
 29         size_t readlen = 0;
 30         readlen = fread(buffer,1,sizeof(buffer),srcFile);
 31         //1代表从srcFile源文件按1字节1字节读
 32         if(readlen == 0)  //判断文件是否读到末尾
 33         {
 34                 break;
 35                 //退出循环
 36         }
 37         size_t writelen = 0;
 38         while(writelen < readlen)
 39         {
 40             writelen += fwrite(buffer + writelen,1,(readlen - writelen),dstFile);  //1字节1字节写入dstFile文件,buffer + writelen表示从上一次处接着写
 41         }
 42
 43     }
 44     fclose(srcFile);
 45     fclose(dstFile);  //最后要把文件关闭,不然会出现难以预料的错误
 46     return 0;
 47 }

 

 

在终端编译运行:gcc fopen.c表示编译该文件,然后会生成一个.o 文件。然后再运行,struct.c是已经存在的源文件,而struct.c.new是目的文件.

然后vim -O struct.c struct.c.new 在终端使用双页显示查看是否拷贝成功,你会发现文件里的内容按字节拷贝过去了。左边是源文件,右边是目的文件

转载于:https://www.cnblogs.com/fookehi/p/3500066.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
线程池的原理已经在上面进行了解释,下面是一个使用线程池实现文件拷贝的C语言示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #define THREAD_POOL_SIZE 5 #define BUFFER_SIZE 1024 typedef struct { char src_file[256]; char dest_file[256]; } CopyTask; typedef struct { pthread_t thread; int is_working; } Worker; CopyTask task_queue[THREAD_POOL_SIZE]; Worker workers[THREAD_POOL_SIZE]; pthread_mutex_t mutex; pthread_cond_t cond; void *worker_thread(void *arg) { while (1) { pthread_mutex_lock(&mutex); // 等待任务到来 while (strlen(task_queue[*(int *)arg].src_file) == 0) { pthread_cond_wait(&cond, &mutex); } // 执行任务 CopyTask copy_task = task_queue[*(int *)arg]; task_queue[*(int *)arg].src_file[0] = '\0'; // 清空任务 pthread_mutex_unlock(&mutex); // 拷贝文件 FILE *src = fopen(copy_task.src_file, "rb"); FILE *dest = fopen(copy_task.dest_file, "wb"); if (src == NULL || dest == NULL) { printf("Failed to open file.\n"); continue; } char buffer[BUFFER_SIZE]; size_t bytesRead; while ((bytesRead = fread(buffer, sizeof(char), BUFFER_SIZE, src)) > 0) { fwrite(buffer, sizeof(char), bytesRead, dest); } fclose(src); fclose(dest); printf("File copied from %s to %s\n", copy_task.src_file, copy_task.dest_file); } } void thread_pool_init() { int i; pthread_mutex_init(&mutex, NULL); pthread_cond_init(&cond, NULL); for (i = 0; i < THREAD_POOL_SIZE; i++) { workers[i].is_working = 0; task_queue[i].src_file[0] = '\0'; pthread_create(&workers[i].thread, NULL, worker_thread, &i); } } void thread_pool_submit(char *src_file, char *dest_file) { pthread_mutex_lock(&mutex); // 查找空闲线程 int i; for (i = 0; i < THREAD_POOL_SIZE; i++) { if (strlen(task_queue[i].src_file) == 0) { strcpy(task_queue[i].src_file, src_file); strcpy(task_queue[i].dest_file, dest_file); pthread_cond_signal(&cond); break; } } pthread_mutex_unlock(&mutex); } int main() { int i; thread_pool_init(); // 提交任务 for (i = 0; i < 10; i++) { char src_file[256], dest_file[256]; sprintf(src_file, "source%d.txt", i); sprintf(dest_file, "destination%d.txt", i); thread_pool_submit(src_file, dest_file); } // 等待任务完成 sleep(1); return 0; } ``` 在上述示例中,我们定义了一个 `CopyTask` 结构体,用于存储拷贝任务的源文件和目标文件。线程池中的任务队列存储了 `CopyTask` 结构体的实例。 在主函数中,我们初始化了线程池,并提交了10个文件拷贝任务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值