IO_day3

#include<myhead.h>
#include <sys/types.h>
#include <dirent.h>
int main(int argc, const char *argv[])
{
	
	DIR* dir = opendir("../exe");	//打开 ../exe 目录
	if(dir == NULL)
	{
		perror("dir open");
		return 1;
	}

	while(1){
	struct dirent* filename = readdir(dir);//循环获取目录下文件信息
	if(filename ==NULL)
	{
		break;
	}
		char *p = filename->d_name;//获取文件名
		
		char pathname[50] = "../exe/";
		
		strcat(pathname,filename->d_name);//拼接目录和文件名生成路经pathname
		

		int rfd = open(pathname,O_RDONLY);//打开读文件获取文件描述符
		
		int wfd = 0;//定义写文件描述符
	
		if(rfd == -1)
		{
			perror("rfd open");
			return 1;
		}	
	
		struct stat statbuf[] = {0};//定义结构体数组
		
		fstat(rfd,statbuf);//将该文件信息写入该结构体数组
			
		mode_t mode = statbuf->st_mode;
  	
		if(S_ISDIR(mode) !=1)//判断该文件是否是文件还是目录,不复制目录
		{
			char copy_name[50] = "copy_";//拼接copy后的文件名字
			strcat(copy_name,filename->d_name);
			wfd = open(copy_name,O_WRONLY | O_CREAT | O_TRUNC,0666);//copy到当前目录
			if(wfd == -1)
			{
				perror("wfd open");
				return 1;
			}
			while(1)
			{
			char buf[20] = {0};
			if(read(rfd,buf,20)==0)
			{
				break;
			}
			write(wfd,buf,strlen(buf));
			}
		}
	close(rfd);
	close(wfd);
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值