小编tow*_*owr的帖子

读写系统调用始终返回1

我需要将一个文件的内容复制到另一个文件.但问题是我一直得到读写返回值1.但是在缓冲区数据中读取.有人可以解释这里出了什么问题吗?我正在使用gcc编译器.

#include<stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include<string.h>
int copy_file_to_file(int,int);
int main()
{
    int src_fd,dst_fd;
    if ( (src_fd = open("source.txt",O_RDONLY)) == -1 )
    {
       printf("file opening error");
       return -1;
    }
    if( (dst_fd = open("destination.txt",O_WRONLY | O_CREAT,0644)) == -1 )
   {
       printf("destination file opening error\n");
       return -1;
   }
   copy_file_to_file(src_fd,dst_fd);
   close(src_fd);
   close(dst_fd);
   return 0;
}

int copy_file_to_file(int source_fd,int dest_fd)
{
    int byte_read, byte_write;
    char buf[10];

    while((byte_read = read(source_fd, buf, 10)>0) )
   {

      byte_write=write(dest_fd, buf, byte_read);
      printf("buf=%s byte_read = %d byte_write=%d \n",buf,byte_read,byte_write); …
Run Code Online (Sandbox Code Playgroud)

c linux

0
推荐指数
1
解决办法
643
查看次数

标签 统计

c ×1

linux ×1