有没有办法直接写入磁盘而无需在c++中进行缓存?
目前,我正在使用:
int open(fileout, O_CREATE | O_WRONLY | O_DIRECT);
FILE *fh = fdopen(fileout_dev, "wb");
是否有跨平台库允许这样做?
谢谢阿曼。
最佳答案
如果您正在谈论缓冲,那么可以使用setbuf或setvbuf
/* setbuf example */
#include <stdio.h>
int main ()
{
char buffer[BUFSIZ];
FILE *pFile1, *pFile2;
pFile1=fopen ("myfile.txt","w");
pFile2=fopen ("myfile2.txt","a");
setbuf ( pFile1 , buffer );
fputs ("This is sent to a buffered stream",pFile1);
fflush (pFile1);
setbuf ( pFile2 , NULL );
fputs ("This is sent to an unbuffered stream",pFile2);
fclose (pFile1);
fclose (pFile2);
return 0;
}
/* setvbuf example */
#include <stdio.h>
int main ()
{
FILE *pFile;
pFile=fopen ("myfile.txt","w");
// _IONBF disables buffering
setvbuf (pFile , NULL , _IONBF, 0);
// File operations here
fclose (pFile);
return 0;
}