开发者

ifstream vs. fread for binary files

开发者 https://www.devze.com 2023-03-08 05:13 出处:网络
Which is faster? ifstream or fread. Which should I use to read binary files? fread() puts the whole file into the memory.

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 har开发者_StackOverflowd 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!


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.


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.


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.


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.


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消