C++ ios :fail() flag

标签 c++ ifstream

我正在尝试读取一个大于 2GB(大约 15GB)的 las 文件,但是 ios::fail() 标志在第 345 个字节处变为真。这是下面的代码。

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

输出是:

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

我错过了什么?

最佳答案

我猜您使用的是 Windows。 Windows 有一个怪癖,即无论文件实际有多大,Control-Z 都会标记文本文件的结尾。解决方案是以二进制模式打开文件。

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

关于C++ ios :fail() flag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16363088/

相关文章:

c++ - 关于 C++ 中临时对象的确切销毁时间的问题

c++ - 一次加载整个缓存行以避免争用其中的多个元素

c++ - 在 C++ 中声明泛型 istream

C++ 无法检查函数内部的 ifstream/ofstream.is_open()

c++ - 使用 fstream 在 C++ 中读取文件时显示额外的字符串

c++ - 模板模板参数推演指南

c++ - 局部变量单元测试

c++ - GetTextExtentPoint32 不考虑当前字体

c++ - 不能将 std::string 设置为等于另一个 std::string 的第零个索引

c++从内存中使用ifstream