#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char const *argv[])
{
//步骤一:定义变量
int fd1,fd2;
int ret;
DIR *dir;
int frc,fde;
struct dirent *dp;
char buff[30] = {0};
char file_path[30] = {0};
char file_name[30] = {0};
//步骤二:从键盘输入文件路径
printf("plase input file path name:\n");
scanf("%s",file_path);
//步骤三:打开目录
dir = opendir(file_path);
if (dir == NULL)
{
printf("opne failed\n");
return -1;
}
printf("open sucess\n");
while (1)
{
dp = readdir(dir);
if (dp != NULL)
{
printf("file name %s:\n",dp->d_name);
}else
{
break;
}
}
//步骤四:获得要复制的路径
printf("plase input file name:\n");
scanf("%s",file_name);
//步骤五:获得文件描述符
frc = open(strcat(strcat(file_path,"/"),file_name) ,O_RDWR);
if (frc < 0)
{
printf("opne file1\n");
return -1;
}
fde = open(file_name,O_RDWR|O_CREAT,0666);
if (fde < 0)
{
printf("opne file2\n");
return -1;
}
//读写操作
while ((ret = read(frc,buff,30))!= 0)
{
write(fde,buff,ret);
};
close(frc);
close(fde);
closedir(dir);
return 0;
}
文件编程小练习(复制目录下的文件到当前路径)
最新推荐文章于 2024-08-23 08:57:21 发布