简体   繁体   中英

Is there an un-buffered I/O in Windows system?

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.

Look at CreateFile with the FILE_FLAG_NO_BUFFERING option

http://www.codeproject.com/Articles/51678/Improve-responsiveness-in-Windows-with-the-FILE_FL

The only method to prevent swapping out cache is to open files with the FILE_FLAG_NO_BUFFERING flag. This, however, requires disk I/O requests to have sizes divisible by sector size (512 to 4096 bytes), which would require large rewrites of most applications that rely on being able to request different sizes.

This project contains a drop-in wrapper that offers the CreateFile_NB() , ReadFile_NB() , WriteFile_NB() , and CloseHandle_NB() functions that take care of queuing and adjusting the file size when closing a file opened for writing.

http://msdn.microsoft.com/en-us/library/cc644950(v=vs.85).aspx

When opening or creating a file with the CreateFile function, the FILE_FLAG_NO_BUFFERING flag can be specified to disable system caching of data being read from or written to the file. Although this gives complete and direct control over data I/O buffering, in the case of files and similar devices there are data alignment requirements that must be considered.

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.

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.

You can use _write MSDN Page Here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM