我正在尝试读取一个大于 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/