如何用c/c++修改磁盘缓存大小
我打算将视频流以文件的方式存储到磁盘上,为了避免磁盘磁头的频繁读写,影响存取数据的效率,现将视频流数据先写入内存缓存再往文件中写,磁盘自身缓存机制,这样的数据其先是写到其本身的文件缓存中,当这个缓存满时,其再自动的写入到文件里,但磁盘默认的缓存大小只有4K(我写了程序测试过),对我现在操作应用感觉这个默认缓存大小有点太小了,所以我想通过程序或API修改这个系统默认的缓存大小,但不知道如何做,有这样的API么?另外,像这样将视频流存储在文件中,用哪种文件操作方式比较好呢?
[解决办法]
setvbuf
Controls stream buffering and buffer size.
int setvbuf( FILE *stream, char *buffer, int mode, size_t size );
Routine Required Header Compatibility
setvbuf <stdio.h> ANSI, Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
setvbuf returns 0 if successful, or a nonzero value if an illegal type or buffer size is specified.
Parameters
stream
Pointer to FILE structure
buffer
User-allocated buffer
mode
Mode of buffering
size
Buffer size in bytes. Allowable range: 2 < size < 32768. Internally, the value supplied for size is rounded down to the nearest multiple of 2.
Remarks
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
With MS-DOS, the same as _IOFBF.
_IONBF
No buffer is used, regardless of buffer or size.
Example
/* SETVBUF.C: This program opens two streams: stream1
* and stream2. It then uses setvbuf to give stream1 a
* user-defined buffer of 1024 bytes and stream2 no buffer.
*/
#include <stdio.h>
void main( void )
{
char buf[1024];
FILE *stream1, *stream2;
if( ((stream1 = fopen( "data1", "a" )) != NULL) &&
((stream2 = fopen( "data2", "w" )) != NULL) )
{
if( setvbuf( stream1, buf, _IOFBF, sizeof( buf ) ) != 0 )
printf( "Incorrect type or size of buffer for stream1\n" );
else
printf( "'stream1' now has a buffer of 1024 bytes\n" );
if( setvbuf( stream2, NULL, _IONBF, 0 ) != 0 )
printf( "Incorrect type or size of buffer for stream2\n" );
else
printf( "'stream2' now has no buffer\n" );
_fcloseall();
}
}
Output
'stream1' now has a buffer of 1024 bytes
'stream2' now has no buffer
Stream I/O Routines
See Also fclose, fflush, fopen, setbuf
[解决办法]
FlushFileBuffers
The FlushFileBuffers function clears the buffers for the specified file and causes all buffered data to be written to the file.
BOOL FlushFileBuffers(
HANDLE hFile // open handle to file whose buffers are to be
// flushed
);
Parameters
hFile
An open file handle. The function flushes this file's buffers. The file handle must have GENERIC_WRITE access to the file.
If hFile is a handle to a communications device, the function only flushes the transmit buffer.
If hFile is a handle to the server end of a named pipe, the function does not return until the client has read all buffered data from the pipe.
Windows NT: The function fails if hFile is a handle to console output. That is because console output is not buffered. The function returns FALSE, and GetLastError returns ERROR_INVALID_HANDLE.
Windows 95: The function does nothing if hFile is a handle to console output. That is because console output is not buffered. The function returns TRUE, but it does nothing.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Remarks
The WriteFile and WriteFileEx functions typically write data to an internal buffer that the operating system writes to disk on a regular basis. The FlushFileBuffers function writes all of the buffered information for the specified file to disk.
You can pass the same file handle used with the _lread, _lwrite, _lcreat, and related functions to FlushFileBuffers.
QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Unsupported.
Header: Declared in winbase.h.
Import Library: Use kernel32.lib.
See Also
File I/O Overview, File Functions, _lread, _lwrite, _lcreat, WriteFile, WriteFileEx
[解决办法]
函数 说明
open() 打开一个文件并返回它的句柄
close() 关闭一个句柄
lseek() 定位到文件的指定位置
read() 块读文件
write() 块写文件
eof() 测试文件是否结束
filelength() 取得文件长度
rename() 重命名文件
chsize() 改变文件长度
以上是非缓冲的文件读写函数,直接写文件的.你自己在内存里实现一个缓冲就ok了.