Linux C语言实现文件夹拷贝 |
您所在的位置:网站首页 › linux系统如何复制文件 › Linux C语言实现文件夹拷贝 |
一、相关函数说明: opendir() 头文件: #include #include 功能: 打开一个目录,在失败的时候返回一个空的指针 函数原型: DIR* opendir(const char * path); 参数: path --- 打开目录路径 返回值: 成功 --- DIR结构体指针 失败 --- NULL readdir() 头文件: #include 功能: 读取一个目录项中的信息 函数原型: struct dirent* readdir(DIR* dp); 参数: dp --- 读取的目录项 返回值: 成功 --- 目录项中的所有信息,存储在dirent结构体中 失败 --- NULL 说明: struct dirent { long d_ino; //索引节点号 off_t d_off; //在目录文件中的偏移 unsigned short d_reclen; //文件名长 unsigned char d_type; //文件类型 char d_name [NAME_MAX+1]; //文件名,最长255字符 } 文件类型有: DT_BLK 块设备文件 This is a block device. DT_CHR 字符设备文件 This is a character device. DT_DIR 目录文件 This is a directory. DT_FIFO 管道文件 This is a named pipe (FIFO). DT_LNK 链接文件 This is a symbolic link. DT_REG 普通文件 This is a regular file. DT_SOCK 网络通信文件 This is a UNIX domain socket. DT_UNKNOWN 未知文件 The file type could not be determined.
closedir() 头文件: #include #include 功能: 关闭一个目录项 函数原型: int closedir(DIR *dir); 参数: dir --- 目录项 返回值: 成功 --- 0 失败 --- -1 二、部分代码/* * 函数名:copy * 功 能:文件拷贝 * 参 数: * srcFilePath --- 源文件路径 * dstFilePath --- 目标文件路径 * 返回值:无 **/ void copy(char *srcFilePath, char *dstFilePath) { int len; FILE *src,*dst; char buf[1024] = {0}; //打开文件 src = fopen(srcFilePath,"r+"); if(!src){ perror("fopen srcFilePath error!"); } dst = fopen(dstFilePath,"w+"); if(!dst){ perror("fopen dstFilePath error!"); } //循环拷贝 while((len = fread(buf,1,1024,src))>0){ fwrite(buf,1,len,dst); } //关闭文件 fclose(src); fclose(dst); }运行命令:./main source dst 三、实现效果后续跟新以下Demo: |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |