二进制文件的 ifstream 与 fread

发布于 2024-11-09 18:01:25 字数 1000 浏览 0 评论 0原文

哪个更快? ifstreamfread
我应该使用哪个来读取二进制文件?

fread() 将整个文件放入内存。
因此,在 fread 之后,访问它创建的缓冲区速度很快。

ifstream::open() 是否将整个文件放入内存?
或者每次我们运行 ifstream::read() 时它都会访问硬盘吗?

那么... ifstream::open() == fread() 吗?
或者 (ifstream::open(); ifstream::read(file_length);) == fread()

或者我应该使用 ifstream::rdbuf()->read() 吗?

编辑: 我的 readFile() 方法现在看起来像这样:

void readFile()
{
    std::ifstream fin;
    fin.open("largefile.dat", ifstream::binary | ifstream::in);
    // in each of these small read methods, there are at least 1 fin.read()
    // call inside.
    readHeaderInfo(fin);
    readPreference(fin);
    readMainContent(fin);
    readVolumeData(fin);
    readTextureData(fin);
    fin.close();
}

小方法中的多个 fin.read() 调用会减慢程序速度吗? 我应该在 main 方法中只使用 1 个 fin.read() 并将缓冲区传递给小方法吗?我想我要写一个小程序来测试一下。

谢谢!

Which is faster? ifstream or fread.
Which should I use to read binary files?

fread() puts the whole file into the memory.
So after fread, accessing the buffer it creates is fast.

Does ifstream::open() puts the whole file into the memory?
or does it access the hard disk every time we run ifstream::read()?

So... does ifstream::open() == fread()?
or (ifstream::open(); ifstream::read(file_length);) == fread()?

Or shall I use ifstream::rdbuf()->read()?

edit:
My readFile() method now looks something like this:

void readFile()
{
    std::ifstream fin;
    fin.open("largefile.dat", ifstream::binary | ifstream::in);
    // in each of these small read methods, there are at least 1 fin.read()
    // call inside.
    readHeaderInfo(fin);
    readPreference(fin);
    readMainContent(fin);
    readVolumeData(fin);
    readTextureData(fin);
    fin.close();
}

Will the multiple fin.read() calls in the small methods slow down the program?
Shall I only use 1 fin.read() in the main method and pass the buffer into the small methods? I guess I am going to write a small program to test.

Thanks!

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

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

发布评论

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

评论(5

俏︾媚 2024-11-16 18:01:25

您真的确定 fread 将整个文件放入内存吗?文件访问可以被缓冲,但我怀疑你是否真的将整个文件放入内存中。我认为 ifstream::read 只是以更符合 C++ 的方式在底层使用 fread (因此是从 C++ 文件中读取二进制信息的标准方法)。我怀疑是否存在显着的性能差异。

要使用fread,文件必须打开。它不仅仅需要一个文件并将其立即放入内存中。所以 ifstream::open == fopenifstream::read == fread

Are you really sure about fread putting the whole file into memory? File access can be buffered, but I doubt that you really get the whole file put into memory. I think ifstream::read just uses fread under the hood in a more C++ conformant way (and is therefore the standard way of reading binary information from a file in C++). I doubt that there is a significant performance difference.

To use fread, the file has to be open. It doesn't take just a file and put it into memory at once. so ifstream::open == fopen and ifstream::read == fread.

穿越时光隧道 2024-11-16 18:01:25

如果您使用高级 api,C++ 流 api 通常比 C 文件 api 慢一点,但它提供比 C 更干净/更安全的 api。
如果您想要速度,请考虑使用内存映射文件,尽管标准库没有可移植的方法来执行此操作。

C++ stream api is usually a little bit slower then C file api if you use high level api, but it provides cleaner/safer api then C.
If you want speed, consider using memory mapped files, though there is no portable way of doing this with standard library.

哭泣的笑容 2024-11-16 18:01:25

至于哪个更快,请看我的评论。对于其余的:

  • 这些方法都不会自动将整个文件读取到内存中。他们都读了你指定的内容。
  • 至少对于 ifstream 来说,我确信 IO 是缓冲的,因此每次读取时不一定都会进行磁盘访问。
  • 有关读取二进制文件的 C++ 方式,请参阅此问题

As to which is faster, see my comment. For the rest:

  • Neither of these methods automatically reads the whole file into memory. They both read as much as you specify.
  • As least for ifstream I am sure that the IO is buffered, so there will not necessarily be a disk access for every read you make.
  • See this question for the C++-way of reading binary files.
能怎样 2024-11-16 18:01:25

C++ 文件流的想法是,部分或全部文件缓冲在内存中(基于它认为是最佳的),并且您不必担心它。

我会使用 ifstream::read() 并告诉它您需要多少。

The idea with C++ file streams is that some or all of the file is buffered in memory (based on what it thinks is optimal) and that you don't have to worry about it.

I would use ifstream::read() and just tell it how much you need.

满意归宿 2024-11-16 18:01:25

使用流运算符:

DWORD processPid = 0;
std::ifstream myfile ("C:/Temp/myprocess.pid", std::ios::binary);
if (myfile.is_open())
{
    myfile >> processPid;
    myfile.close();
    std::cout << "PID: " << processPid << std::endl;
}

Use stream operator:

DWORD processPid = 0;
std::ifstream myfile ("C:/Temp/myprocess.pid", std::ios::binary);
if (myfile.is_open())
{
    myfile >> processPid;
    myfile.close();
    std::cout << "PID: " << processPid << std::endl;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文