利用线程池书写文件夹复制(C语言)

如题

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdbool.h>
#include<unistd.h>
#include<string.h>
#include<strings.h>
#include<errno.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<dirent.h>
#include<time.h>

#define PTHREAD_MAX		20	//最多有20个线程
#define TASK_FULL_NUM	100	//最多等待100个任务

//任务链表结构体
struct task{
	void *(*task_func)(char* from_adr,char* to_adr);//任务函数
//	int task_arg;//任务函数的参数
	char src[300];
	char dst[300];
	struct task *next;
};


//线程池
struct pthread_pool{
	pthread_t tid[PTHREAD_MAX];//线程号数组
	
	struct task *head;//任务链表的头节点
	
	unsigned int active_pthread_num;//正在运行的线程个数
	unsigned int wait_task_num;//正在等待的任务个数
	
	pthread_mutex_t m;//线程池互斥锁
	pthread_cond_t v;//线程池条件变量
	
	bool shutdown;//线程销毁标志位 flase:不销毁   true:销毁
};

int is_dir( char* file_name);
int endwith(char* s,char c);
//void cp_file( char *source_path , char *destination_path);
void copy_folder(char* source_path,char *destination_path,struct pthread_pool *pool);
int is_dir( char* file_name);
void *f(char* from_adr , char* to_adr);
void cleanup(void *arg);
void *func(void *arg);
struct pthread_pool* pthread_pool_init();
int pthread_pool_add(struct pthread_pool *pool,int addnum);
int pool_add_task(struct pthread_pool *pool,void *(*f)(char* src ,char* dst),char* src,char* dst );
int pool_delete(struct pthread_pool *pool);
// void cp_file( char *source_path , char *destination_path)
// {
	
// }
int endwith(char* s,char c){//用于判断字符串结尾是否为“.”
	printf("end : %c\n",s[strlen(s)-1]);
	if(s[strlen(s)-1]==c){
		return 1;
	}
	else{
		return 0;
	}
}

void copy_folder(char* source_path,char *destination_path,struct pthread_pool *pool)
{
	//printf(" your dir is correct !\n");// remind user input dir is correct	
	DIR *dst_dp = opendir(destination_path);
	if(dst_dp  == NULL )
	{
		printf(" your dest dir is not existed \n");
		printf(" system will mkdir for U \n");
		if(mkdir(destination_path,0777) == -1)
		{
			printf(" error occur during mkdir");
			exit(-1);
		}
	}
	DIR *src_dp = opendir(source_path);	
	struct dirent *ep_src 
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言中,可以使用以下步骤来复制文件夹并生成新的文件夹: 1. 使用 `opendir()` 函数打开要复制文件夹。 2. 使用 `mkdir()` 函数创建新的文件夹。 3. 使用 `readdir()` 函数遍历要复制文件夹中的所有文件和子文件夹。 4. 对于每个文件和子文件夹: - 如果是文件,则使用 `fopen()` 函数打开原始文件和新文件,并使用 `fread()` 和 `fwrite()` 函数将原始文件的内容复制到新文件中。 - 如果是子文件夹,则递归调用复制文件夹的函数。 5. 使用 `closedir()` 函数关闭原始文件夹。 以下是一个简单的示例代码,可以复制一个文件夹并生成新的文件夹: ```c #include <stdio.h> #include <dirent.h> #include <sys/stat.h> #include <string.h> void copy_folder(char* src_folder, char* dest_folder) { DIR* src_dir = opendir(src_folder); if(src_dir) { mkdir(dest_folder, 0777); struct dirent* dir; while((dir = readdir(src_dir)) != NULL) { if(strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) { continue; } char src_path[1000]; char dest_path[1000]; sprintf(src_path, "%s/%s", src_folder, dir->d_name); sprintf(dest_path, "%s/%s", dest_folder, dir->d_name); struct stat statbuf; if(stat(src_path, &statbuf) == -1) { continue; } if(S_ISDIR(statbuf.st_mode)) { copy_folder(src_path, dest_path); } else { FILE* src_file = fopen(src_path, "rb"); FILE* dest_file = fopen(dest_path, "wb"); if(src_file && dest_file) { char buffer[1000]; size_t bytes; while((bytes = fread(buffer, 1, sizeof(buffer), src_file)) > 0) { fwrite(buffer, 1, bytes, dest_file); } fclose(src_file); fclose(dest_file); } } } closedir(src_dir); } } int main() { copy_folder("path/to/src/folder", "path/to/dest/folder"); return 0; } ``` 请注意,此示例仅复制文件夹中的文件和子文件夹,而不包括文件夹本身。如果要复制整个文件夹(包括文件夹本身),则可以在 `mkdir()` 函数之前使用 `mkdir()` 函数创建新的文件夹

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值