read/write循环读写数据

//循环读
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;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天未及海宽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值