B
B
byreoil2018-07-30 00:33:46
C++ / C#
byreoil, 2018-07-30 00:33:46

Why doesn't C++ ifstream::read read the entire binary file?

Hello!
I ran into a problem when working with a binary file in C++.
Here is a code snippet:

//! Get memory to File
    bool MemFromFile(const char * strFileName, std::vector<char> &DataOut)
    {
        DataOut.clear(); // clearing data in vector for add new data

        std::ifstream inFile(strFileName); // open file
        if(!inFile.is_open())
            return false; // Can't create or read file
    
    do
    {
      char Buf = '\0';
      if (inFile.read(&Buf, 1))
        DataOut.push_back(Buf);
    } while (inFile);
    
        inFile.close(); // close file
        return true;
    }

Here is the binary file itself:
5b5e3201a37e8330583822.png
Reads only the first 3 characters from the file (does not read after 2a).
This problem manifests itself under windows, Visual Studio 2015 IDE, I checked the g++ compiler under linux (Ubuntu 14), and everything works fine.
UPD: I made the source code through the tag

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2018-07-30
@byreoil

The character code 0x1A in Windows (and earlier in DOS) is recognized as the end of a text file (EOF).
Open the file in binary mode
And for the future - the code must be formatted with a tag, and not as a picture (see Rules , clause 3.8)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question