Reputation: 955
I need to copy content of one file to another. But the problem is I am getting the read & write return value as always 1. But in buffer data is read. Can someone please explain what going wrong here? I am using gcc compiler.
#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);
}
return 0;
}
output:-
buf=Hello world
I am working
byte_read = 1
byte_write=1
Upvotes: 0
Views: 1381
Reputation: 75727
while((byte_read = read(source_fd, buf, 10)>0) )
is interpreted as (br = (read() > 0))
fix:
while((byte_read = read(source_fd, buf, 10)) > 0)
Upvotes: 9