views:

670

answers:

4

I want to find low-level C/C++ APIs, equivalent with "write" in linux systems, that don't have a buffer. Is there one?

The buffered I/O such as fread, fwrite are not what I wanted.

+2  A: 

streams are about as low level as you can get.. and they can be unbuffered.

int setvbuf(
   FILE *stream,
   char *buffer,
   int mode,
   size_t size 
);

example

  setvbuf(stdout, (char *)NULL, _IONBF, 0); //unbuffered stdout

here is an extract from the vc2008 help document.

The setvbuf function allows the program to control both buffering and buffer size for stream. stream must refer to an open file that has not undergone an I/O operation since it was opened. The array pointed to by buffer is used as the buffer, unless it is NULL, in which case setvbuf uses an automatically allocated buffer of length size/2 * 2 bytes.

The mode must be _IOFBF , _IOLBF , or _IONBF. If mode is _IOFBF or _IOLBF, then size is used as the size of the buffer. If mode is _IONBF, the stream is unbuffered and size and buffer are ignored. Values for mode and their meanings are:

_IOFBF Full buffering; that is, buffer is used as the buffer and size is used as the size of the buffer. If buffer is NULL, an automatically allocated buffer size bytes long is used.

_IOLBF For some systems, this provides line buffering. However, for Win32, the behavior is the same as _IOFBF - Full Buffering.

_IONBF No buffer is used, regardless of buffer or size.

ShoeLace
This I believe is the buffering in the C libraries. It does not influence OS level or hardware level buffering.
Blank Xavier
+11  A: 

Look at CreateFile with the FILE_FLAG_NO_BUFFERING option

Shay Erlichmen
+4  A: 

The Win32 equivalence of the POSIX write() function is WriteFile(). The documentation recommends using un-buffered file I/O, and recommends this page for further information.

unwind
+2  A: 

You can use _write MSDN Page Here.

Jason Coco