Hi,
I am trying to write a program to copy files using aync I/O in unix.
I have written this code :
Code:
#include<stdio.h>
#include<sys/types.h>
#include<sys/uio.h>
#include<unistd.h>
int main(int argc,char *argv[])
{
int fs,fd;
ssize_t bytes_read,bytes_written;
char buf[3][50];
int iovcnt;
struct iovec iov[3];
int i;
fs=open(argv[1],O_RDONLY);
fd=open(argv[2],O_RDWR | O_CREAT);
for(i=0;i<3;i++)
{
iov[i].iov_base=buf[i];
iov[i].iov_len=sizeof(buf[i]);
}
iovcnt=sizeof(iov)/sizeof(struct iovec);
while((bytes_read=readv(fs,iov,iovcnt))!=-1)
if((bytes_written=writev(fd,iov,iovcnt))==-1)
printf("Write error\n");
close(fs);
close(fd);
return 0;
}
The contents of source file are getting copied to the destination but some junk is also appended with it. I suspect the problem is because of the buffers used for writing. The extra space in the buffers have junk which is written to the file. Can anyone suggest a fix for this?