I have this copying code from infile to outfile,
the problem is that there is a lot of garbage added at the end of the outfile
ssize_t nread;
int bufsize=512;
char buffer[bufsize];
while ( (nread=read(infile, buffer, bufsize)>0))
{
if( write(outfile, buffer, bufsize)<nread )
{
close(outfile); close(infile); printf("error in write loop !\n\n");
return (-4);
}
}
if( nread == -1) {
printf ("error on last read\n"); return (-5);
}//error on last read /
what should i do to fix this ?
should be:
as
>has higher precedence when compared to=.Also
you are always writing
bufsizenumber of bytes. But there need not be those many bytes read in the read operation. This could happen in the last iteration of the copying. To fix this you should be writingnreadnumber of bytes.