What are the main differences between fwrite and write?

c, c++

Solution

`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.

Problem

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 written = fwrite(ptr, size, nmemb, (FILE)*stream); return written; } ``` This function is going to be used in another function, which does a `HTTP` request, retrieves the request, and writes it to the local machine. That `writedata` function will be used for the later part. The whole operation has to be `multithreaded`, so I was in doubt between `write` and `fwrite`. Could someone help me outlining the differences between `write()` and `fwrite()` in `C`, so I could choose which one best fits into my problem?

Original source

Related problems