Windows系统中是否有无缓冲的I/O?
我想找到低级 C/C++ API,相当于 Linux 系统中的“写入”,但没有缓冲区。 有吗?
fread、fwrite 等缓冲 I/O 不是我想要的。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我想找到低级 C/C++ API,相当于 Linux 系统中的“写入”,但没有缓冲区。 有吗?
fread、fwrite 等缓冲 I/O 不是我想要的。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
查看带有 FILE_FLAG_NO_BUFFERING 选项的 CreateFile
Look at CreateFile with the FILE_FLAG_NO_BUFFERING option
http://www.codeproject.com/Articles /51678/Improve-responsiveness-in-Windows-with-the-FILE_FL
防止换出缓存的唯一方法是使用
FILE_FLAG_NO_BUFFERING
标志打开文件。 然而,这要求磁盘 I/O 请求的大小可以被扇区大小(512 到 4096 字节)整除,这将需要对大多数依赖于请求不同大小的应用程序进行大量重写。该项目包含一个嵌入式包装器,提供
CreateFile_NB()
、ReadFile_NB()
、WriteFile_NB()
和CloseHandle_NB( )
函数,在关闭为写入而打开的文件时负责排队和调整文件大小。http://msdn.microsoft.com/en- us/library/cc644950(v=vs.85).aspx
使用 CreateFile 函数打开或创建文件时,可以指定 FILE_FLAG_NO_BUFFERING 标志以禁用系统缓存正在读取的数据来自或写入文件。 尽管这可以完全且直接地控制数据 I/O 缓冲,但对于文件和类似设备,必须考虑数据对齐要求。
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()
, andCloseHandle_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.POSIX write() 函数的 Win32 等效项为
此页面了解更多信息。
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.流的级别是尽可能低的。
并且它们可以是无缓冲的。
这里的例子
摘自vc2008帮助文档。
setvbuf 函数允许程序控制流的缓冲和缓冲区大小。 流必须引用自打开以来未经历过 I/O 操作的打开文件。 buffer 指向的数组用作缓冲区,除非它为 NULL,在这种情况下 setvbuf 使用长度为 size/2 * 2 字节的自动分配的缓冲区。
模式必须为 _IOFBF 、 _IOLBF 或 _IONBF。 如果模式为_IOFBF或_IOLBF,则大小将用作缓冲区的大小。 如果模式为 _IONBF,则流是无缓冲的,并且大小和缓冲区将被忽略。 模式的值及其含义为:
_IOFBF
全缓冲; 即使用buffer作为缓冲区,使用size作为缓冲区的大小。 如果 buffer 为 NULL,则使用自动分配的缓冲区大小字节长。
_IOLBF
对于某些系统,这提供了行缓冲。 但是,对于 Win32,行为与 _IOFBF - 完全缓冲相同。
_IONBF
无论缓冲区或大小如何,都不使用缓冲区。
streams are about as low level as you can get..
and they can be unbuffered.
example
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.
您可以使用
_write
MSDN 页面 。You can use
_write
MSDN Page Here.