linux文件操作函数read,!linux 文件操作 read函数为什么总是读不出数据,哪里出错了...

求救!linux 文件操作 read函数为什么总是读不出数据,哪里出错了

本帖最后由 jj2060 于 2014-11-06 16:59:35 编辑

下面这段代码没有报错,但是就是读不出文件中的数据,求高手指教,弄了好久,就是弄不出来

#include 

#include 

#include 

#include 

#include 

#include 

#define CREATE_MODE     0755

#define BUFF_SIZE       1024

int open_file(char *filename);

void rw_file(int from_fd, int to_fd, char *src_file, char *det_file);

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

int i = 0;

char *src_file;

char *det_file;

int from_fd;

int to_fd;

if(argc 

perror("You haven't input the filename ,please try again!\n");

exit(EXIT_FAILURE);

}else{

src_file = argv[1];

det_file = argv[2];

from_fd  = open_file(src_file);

to_fd    = open_file(det_file);

rw_file(from_fd, to_fd, src_file, det_file);

//              rw_file(open(argv[1]), open(argv[2]));

}

exit(EXIT_SUCCESS);

return 0;

}

int open_file(char *filename){

int fd = 0;

if((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IXUSR))<0){

printf("open file %s failure!\n",filename);

exit(EXIT_FAILURE);

}else{

printf("open file %s success, fd:%d\n",filename,fd);

}

//      if(close(fd) == 0){

//

//              printf("close file %s success,fd:%d\n",filename,fd);

//      }else{

//              printf("close file %s failure,fd:%d\n",filename,fd);

//      }

return fd;

}

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

int read_size  = 0;

int write_size = 0;

char buff[BUFF_SIZE] = { 0 };

while((read_size = read(from_fd, buff, BUFF_SIZE)) > 0 ){

if(read_size == -1){

break;

}

printf("read file %s success,from_fd:%d,read_size:%d",src_file,from_fd,read_size);

write_size = write(to_fd, buff, read_size);

while(write_size 

read_size  -= write_size;

write_size += write(to_fd, buff, read_size);

printf("write file %s success,to_fd:%d,write_size:%d",det_file,to_fd,write_size);

}

}

close(from_fd);

close(to_fd);

return;

}

我调试过,就是在下面这行时,直接跳到close函数,好像根本没有读数据返回的值好像是-1

while((read_size = read(from_fd, buff, BUFF_SIZE)) > 0 )

------解决思路----------------------

if((fd = open(filename, O_WRONLY

------解决思路----------------------

O_CREAT, S_IRUSR

------解决思路----------------------

S_IWUSR

------解决思路----------------------

S_IXUSR))<0){

===>

if((fd = open(filename, O_RDWR

------解决思路----------------------

O_CREAT, S_IRUSR

------解决思路----------------------

S_IWUSR

------解决思路----------------------

S_IXUSR))<0){

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值