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?
fwritewrites to aFILE*, i.e. a (potentially) bufferedstdiostream. It’s specified by the ISO C standard. Additionally, on POSIX systems,fwriteis thread-safe to a certain degree.writeis 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 aFILE*, then fetch its file descriptor withfileno, but be sure to manually lock and flush the stream before attempting awrite.Use
fwriteunless you know what you’re doing.