//循环读
size_t myRead(int fd, char *buf, int len)
{
int n = 0;
int r = 0;
while (r < len) {
n = read (fd, buf + r, len - r);
if (n <= 0)
return r;
r += n;
}
return r;
}
//循环写
size_t myWrite (int fd, char *buf, int len)
{
int n = 0;
int r = 0;
while (r < len) {
n = write (fd, buf + r, len - r);
if (n < 0)
return n;
r += n;
}
return r;
}
//边读边写 读srcPath下的文件 写入exportPath
int copy_file(char *srcPath, char *exportPath)
{
if(access(srcPath, F_OK) != 0 && access(dstPath, F_OK) != 0)
return -1;
int src_fd ,export_fd;
if((src_fd = open(srcPath, O_RDONLY)) < 0){
perror("srcPath open failed!\n");
return -1;
}
if((export_fd = open(exportPath, O_RDWR | O_CREAT | O_BINARY, 0755)) < 0){ //以读写+没有就创建+二进制格式打开
perror("exportPathopen open failed!\n");
return -1;
}
struct stat sb;
if (stat(srcPath, &sb) == -1) {
perror("stat");
return -1;
}
#define BUF_SIZE 4096
int r = 0, rRet = 0, wRet = 0;
int src_len = sb.st_size; //获取读的文件大小
char buf[BUF_SIZE] = {0};
while (r < src_len) {
memset(buf, 0, BUF_SIZE);
rRet = read (src_fd, buf, BUF_SIZE);
if(rRet > 0){ //读多少写多少
wRet = write (export_fd, buf, rRet);
if(wRet <= 0 ){ //写出错
break;
}
}else{ //读完
break;
}
r += rRet;
}
return 0;
}
补充:
移动文件比拷贝文件块,移动文件时,只要不是跨磁盘,只需要改一下fat表,不存在文件读写;
所有在没有跨磁盘时,上面copy_file()接口可用rename()函数代替;
// 处理文件名相同
{
char exportPath[FULL_FILE_NAME_LEN_MAX] = {0}; // 如:exportPath/xxx.c
char srcPath[FULL_FILE_NAME_LEN_MAX] = {0}; // /srcPath/test/demo.c
char dirPath[FULL_FILE_NAME_LEN_MAX] = {0}; //如 /srcPath/test/
DIR *pdir;
struct dirent *pdirent;
//... 得到 dirPath
pdir = opendir(dirPath);
if(pdir){ //打开成功
while( (pdirent = readdir(pdir)) != NULL )
{
if ( strcmp(pdirent->d_name, ".") == 0
|| strcmp(pdirent->d_name, "..") == 0
)
{
continue; //过滤 .和..
}
// 保证文件名一样
sprintf(srcPath,"%s/%s",dirPath, pdirent->d_name);
sprintf(exportPath,"%s/%s",dirPath,pdirent->d_name);
ret = copy_file(srcPath, exportPath);
}
}
}
补充:跨文件夹复制文件
int copy_file(const char *srcPath, const char *dstPath, int isDelete)
{
if(access(srcPath, F_OK) != 0 && access(dstPath, F_OK) != 0){
return -1;
FILE *src_fd ,*export_fd;
if(NULL == (src_fd = fopen(srcPath,"r"))){
printf("srcPath open failed!\n");
return -1;
}
if(NULL == (export_fd = fopen(dstPath, "w"))){
printf("exportPathopen open failed!\n");
return -1;
}
int rc = fseek(src_fd, 0, SEEK_END);
int src_len = ftell(src_fd);
fseek(src_fd, 0, SEEK_SET);
#define BUF_SIZE 4096
int r = 0, rRet = 0, wRet = 0;
char buf[BUF_SIZE] = {0};
while (r < src_len) {
memset(buf, 0, BUF_SIZE);
rRet = fread (buf, 1, BUF_SIZE, src_fd);
if(rRet > 0){ //读多少写多少
wRet = fwrite (buf, 1, rRet, export_fd);
if(wRet <= 0 ){ //写出错
break;
}
}else{ //读完
break;
}
r += rRet;
}
if(isDelete)
remove(srcPath);//删除原文件
}
补充:
移动文件比拷贝文件块,移动文件时,只要不是跨磁盘,只需要改一下fat表,不存在文件读写;
所有在没有跨磁盘时,上面copy_file()接口可用rename()函数代替;
额外:
//判断目录是否存在,0表存在,-1不存在
int is_dir_exist(const char *dir_path)
{
if(NULL == dir_path)
return -1;
DIR *dir = NULL;
if((dir = opendir(dir_path)) == NULL)
return -1;
closedir(dir);//打开的最好关闭
return 0;
}
//判断文件是否存在,0表存在,-1不存在
int is_file_exist(const char *file_path)
{
if(NULL == file_path)
return -1;
if(access(file_path, F_OK) == 0)
return 0;
return -1;
}