Setvbuf for file descriptor created by open in C++?

android, c, c++

Solution

A file associated with an `int` is an operating system handle. `setvbuf()` manages buffers with a C runtime library `FILE`.

To prevent buffering, you would have to use the proper operating system specific function, which perhaps can be done when the file is opened. For example, on Linux

int fd = open ("/dev/whatever", O_APPEND | O_WRONLY | O_DIRECT);

To flush data already written, use `fsync()`:

#include <unistd.h>

...
fsync(fd);

Problem

Does anyone know how to set an int file descriptor buffer to no buffer and to flush immediately? I've tried to use `setvbuf` but it takes a `FILE*` not `int fd`. Kevin

Original source