What is the difference between read() and fread()?

前端 未结 6 1297
你的背包
你的背包 2020-11-27 12:45

I\'m reading source code of the linux tool badblocks. They use the read() function there. Is there a difference to the standard C fread() function?

6条回答
  •  隐瞒了意图╮
    2020-11-27 13:34

    One difference you should be aware of if you are converting code that uses one to using the other:

    • fread blocks until the number of bytes you asked for has been read, or the file ends, or an error occurs.
    • read also blocks, but if you ask for say 4kB it may return after reading just 1kB, even if the file has not ended.

    This can cause subtle bugs, as it depends on where the file is stored, caches, etc.

提交回复
热议问题