Snowfish
Snowfish

Reputation: 7737

ifstream vs. fread for binary files

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!

Upvotes: 6

Views: 15013

Answers (5)

zuko
zuko

Reputation: 470

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

Upvotes: -1

Bj&#246;rn Pollex
Bj&#246;rn Pollex

Reputation: 76778

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.

Upvotes: 3

Nekuromento
Nekuromento

Reputation: 2235

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.

Upvotes: 3

Christian Rau
Christian Rau

Reputation: 45948

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.

Upvotes: 5

Steve Blackwell
Steve Blackwell

Reputation: 5924

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.

Upvotes: 0

Related Questions