#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;
}
IO_day3
于 2024-07-17 19:55:54 首次发布