Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read() return "incorrect" value

Tags:

c

linux

while(  rd = read(fd1, buf, 512) != 0)
{
    len += rd;
    if(readed < 0) 
    perror("read: ");
}

MAN pages says, that read() returns number of read bytes, but in my case this code returns number of blocks(depends of 3rd argument) or number of iterations. for example, I have file with 36 symbols and this code returns 1, when all symbols normally read, if I change 512 to 4, it will return 9, and so on. Please correct me and this code to return number of bytes which read with 512 size blocks

like image 398
Ivan Avatar asked Feb 19 '26 14:02

Ivan


1 Answers

rd = read(fd1, buf, 512) != 0

means

rd = (read(fd1, buf, 512) != 0)

The result of != is always either 0 or 1.

You probably meant

while ((rd = read(fd1, buf, 512)) != 0)
like image 91
aschepler Avatar answered Feb 22 '26 07:02

aschepler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!