C++ ios:fail() flag

I am trying to read a las file larger then 2GBs (about 15GBs) but ios::fail() flag becomes true in 345th byte. Here is the code below.

void Foo()
{
  char* filename = "../../../../../CAD/emi/LAS_Data/AOI.las";
  ifstream m_file (filename);

  char c;
  int count = 0;

  if (m_file.is_open())
  {
      while ( m_file.good() )
      {
          m_file.get(c);
          cout << c << endl;
          count++;
      }

      // Check State

      if(m_file.fail())
          cout << "File Error: logical error in i/o operation." << endl;

      if(m_file.eof())
          cout << "Total Bytes Read: " << count << endl;

      m_file.close();
  }
  else
  {
      cout << "File Error: Couldn't open file: " << endl;
  }
}

And the output is:

...
File Error: logical error in i/o operation.
Total Bytes Read: 345

What am I missing?

Upvotes: 1

Views: 1050

Answers (1)

Mark Ransom
Mark Ransom

Reputation: 308091

I'm going to guess that you're using Windows. Windows has a quirk that a Control-Z marks the end of a text file, no matter how large the file actually is. The solution is to open the file in Binary mode.

ifstream m_file (filename, std::ios::binary);

Upvotes: 4

Related Questions