文件读写

来源:互联网 发布:alphago软件 编辑:程序博客网 时间:2024/04/29 07:54
FILE_FLAG_NO_BUFFERING

指示系统不要缓冲,它如果和FILE_FLAG_OVERLAPPED联合使用,将呈现最好的异步性能,因为I/O操作并不依赖于内存管理器的同步性。但是有时I/O操作会慢些,因为没用cache。有时程序需要做调整,比如文件大小必须是扇区大小的整数倍,Buffer地址的按扇区地址对齐等。按扇区地址对齐内存边界可以使用VirtualAlloc来分配内存,GerDiskFreeSpace函数可以得到磁盘一个扇区的大小。

转自CSDN某个论坛


FILE_FLAG_NO_BUFFERING 
 Instructs the system to open the file with no intermediate buffering or caching. When combined with FILE_FLAG_OVERLAPPED, the flag gives maximum asynchronous performance, because the I/O does not rely on the synchronous operations of the memory manager. However, some I/O operations will take longer, because data is not being held in the cache. 
An application must meet certain requirements when working with files opened with FILE_FLAG_NO_BUFFERING: 

File access must begin at byte offsets within the file that are integer multiples of the volume's sector size. 
File access must be for numbers of bytes that are integer multiples of the volume's sector size. For example, if the sector size is 512 bytes, an application can request reads and writes of 512, 1024, or 2048 bytes, but not of 335, 981, or 7171 bytes. 
Buffer addresses for read and write operations must be sector aligned (aligned on addresses in memory that are integer multiples of the volume's sector size). 
One way to align buffers on integer multiples of the volume sector size is to use VirtualAlloc to allocate the buffers. It allocates memory that is aligned on addresses that are integer multiples of the operating system's memory page size. Because both memory page and volume sector sizes are powers of 2, this memory is also aligned on addresses that are integer multiples of a volume's sector size. 

An application can determine a volume's sector size by calling the GetDiskFreeSpace function. 


API:WriteFile的第4个参数,返回写到文件中的字节数。判断这个字节数和要写入的缓冲长度是否相等再结合WriteFile函数的返回值,如果成功的写到了文件中,可以不用每次调用FlushFileBuffers。

0 0