what are the main differences between fwrite and write?

前端 未结 3 2046
天涯浪人
天涯浪人 2020-12-29 11:05

I\'m currently writing a callback function in C:

static size_t writedata(void *ptr, size_t size, size_t nmemb, void *stream){

        size_t wr         


        
3条回答
  •  离开以前
    2020-12-29 11:15

    fwrite writes to a FILE*, i.e. a (potentially) buffered stdio stream. It's specified by the ISO C standard. Additionally, on POSIX systems, fwrite is thread-safe to a certain degree.

    write is a lower-level API based on file descriptors, described in the POSIX standard. It doesn't know about buffering. If you want to use it on a FILE*, then fetch its file descriptor with fileno, but be sure to manually lock and flush the stream before attempting a write.

    Use fwrite unless you know what you're doing.

提交回复
热议问题