免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2002 | 回复: 1
打印 上一主题 下一主题

[C] 求救!linux中的read函数为什么读不出数据 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-11-06 17:06 |只看该作者 |倒序浏览
下面是一个拷贝文件的函数,编译时也没有报错,但是却读不出数据
我调试过了,在执行while((read_size = read(from_fd, buff, BUFF_SIZE)) > 0 )时,直接跳到close了,求高手指教,实在弄不明白
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #define CREATE_MODE     0755
  8. #define BUFF_SIZE       1024
  9. int open_file(char *filename);
  10. void rw_file(int from_fd, int to_fd, char *src_file, char *det_file);

  11. int main(int argc, char *argv[]){

  12.         int i = 0;
  13.         char *src_file;
  14.         char *det_file;
  15.         int from_fd;
  16.         int to_fd;
  17.         if(argc < 3){

  18.                 perror("You haven't input the filename ,please try again!\n");
  19.                 exit(EXIT_FAILURE);
  20.         }else{
  21.                 src_file = argv[1];
  22.                 det_file = argv[2];
  23.                 from_fd  = open_file(src_file);
  24.                 to_fd    = open_file(det_file);
  25.                 rw_file(from_fd, to_fd, src_file, det_file);

  26. //              rw_file(open(argv[1]), open(argv[2]));
  27.         }

  28.         exit(EXIT_SUCCESS);
  29.         return 0;
  30. }

  31. int open_file(char *filename){

  32.         int fd = 0;

  33.         if((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IXUSR))<0){
  34.                 printf("open file %s failure!\n",filename);
  35.                 exit(EXIT_FAILURE);
  36.         }else{
  37.                 printf("open file %s success, fd:%d\n",filename,fd);
  38.         }
  39. //      if(close(fd) == 0){
  40. //
  41. //              printf("close file %s success,fd:%d\n",filename,fd);
  42. //      }else{
  43. //              printf("close file %s failure,fd:%d\n",filename,fd);
  44. //      }

  45.         return fd;
  46. }

  47. void rw_file(int from_fd, int to_fd, char *src_file, char *det_file){

  48.         int read_size  = 0;
  49.         int write_size = 0;
  50.         char buff[BUFF_SIZE] = { 0 };
  51.         while((read_size = read(from_fd, buff, BUFF_SIZE)) > 0 ){
  52.                 if(read_size == -1){
  53.                         break;
  54.                 }
  55.                 printf("read file %s success,from_fd:%d,read_size:%d",src_file,from_fd,read_size);
  56.                 write_size = write(to_fd, buff, read_size);
  57.                 while(write_size < read_size){

  58.                         read_size  -= write_size;
  59.                         write_size += write(to_fd, buff, read_size);
  60.                         printf("write file %s success,to_fd:%d,write_size:%d",det_file,to_fd,write_size);
  61.                 }
  62.         }
  63.         close(from_fd);
  64.         close(to_fd);
  65.         return;
  66. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2014-11-06 22:38 |只看该作者
没人回复,自己回复,原来是if((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IXUSR))<0)这个地方出错了,弄了半天,写错了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP