Ifstream vs. fread for binary files

Which is faster? ifstreamor fread.
What should I use to read binary files?

fread()puts the entire file into memory.
Therefore, after freadaccess to the created buffer is fast.

Does the ifstream::open()entire file include memory?
or does he gain access to the hard drive every time we run ifstream::read()?

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

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

edit Now my readFile () method 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();
}

fin.read() ? 1 fin.read() ? , .

!

+3
5

, fread ? , , , . , ifstream::read fread ++- (, , ++). , .

fread, . . ifstream::open == fopen ifstream::read == fread.

+3

, , . . :

  • . , .
  • ifstream , IO , .
  • . ++ - .
+2

C++ api , C api, api , / api, C. , , .

+1

The idea with C ++ file streams is that some or all of the files are buffered in memory (based on what, in his opinion, is optimal) and that you do not need to worry about that.

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

0
source

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
source

All Articles