简体   繁体   English

read()返回“不正确”的值

[英]read() return “incorrect” value

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. MAN页面说,read()返回读取字节数,但在我的情况下,此代码返回块数(取决于第三个参数)或迭代次数。 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. 例如,我有36个符号的文件,当所有符号通常读取时,此代码返回1,如果我将512更改为4,则返回9,依此类推。 Please correct me and this code to return number of bytes which read with 512 size blocks 请更正我和此代码以返回使用512个大小的块读取的字节数

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

means 手段

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

The result of != is always either 0 or 1 . !=的结果总是01

You probably meant 你可能意味着

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

Incorrect use of brackets, Pl. 不正确使用括号,Pl。 change your code like below. 像下面一样更改你的代码。 Hope It will give you the correct result. 希望它会给你正确的结果。

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

Did you try to set th rd value outside of while loop condition? 您是否尝试将此值设置为while循环条件之外? Without more bracket i think the Setting and testing the value section in the while condition is not correct. 如果没有更多括号,我认为在while条件下设置和测试值部分是不正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM