在 C++ 中直接文件访问?

发布于 2024-11-03 21:41:35 字数 192 浏览 0 评论 0原文

有没有办法在c++中直接写入磁盘而不使用缓存? 目前我正在使用:

int open(fileout, O_CREATE | O_WRONLY | O_DIRECT);  
FILE *fh = fdopen(fileout_dev, "wb");

是否有跨平台库允许这样做?

谢谢阿曼。

Are there way to write directly to disk without cache in c++?
Currently I am using:

int open(fileout, O_CREATE | O_WRONLY | O_DIRECT);  
FILE *fh = fdopen(fileout_dev, "wb");

Are there cross platform libraries allowing to do that?

Thanks Arman.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏雨凉 2024-11-10 21:41:35

如果您正在谈论缓冲,那么您可以使用 setbufsetvbuf

/* 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;
}

If you're talking about buffering, then you can use setbuf or 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文