【问题标题】:Different results when oppening a binary .ply file with ifstream under windows and linux在 windows 和 linux 下使用 ifstream 打开二进制 .ply 文件时的不同结果
【发布时间】:2017-06-27 18:45:29
【问题描述】:

我正在尝试使用 c++ 读取.ply 文件并将几何信息保存在向量中(边界点为floats,边界三角形为int's。代码在Linux 下工作,但当我尝试使用时它在 Windows 下的行为不符合预期。

这是一个精简版的代码:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>

using namespace std;

string FilenamePLY;

int NumberBorderPoint = 1572866;
int BorderNumberTriangle = 3145728;

char numFloat;
char numInt;

int main(int argc, char** argv)
{
    FilenamePLY = "file_test.ply";

    ifstream fin(FilenamePLY.c_str());

    for (int i = 0; i < NumberBorderPoint; i++){
            fin.read(&numFloat, sizeof(float));
            for (int j = 0; j < 3; j++) {
                fin.read(&numFloat, sizeof(float));
            }

    }
    cout << fin.gcount() << endl;

    for (int i = 0; i<BorderNumberTriangle; i++){
            fin.read(&numInt, sizeof(int));
            for (int j = 0; j<3; j++)   {
                fin.read(&numInt, sizeof(int));
            }
    }
    cout << fin.gcount() << endl;
    return 0;
}

Windows下编译执行的代码输出:

0
0

而在 Linux 下的输出是:

4
4

我的感觉是 read 函数没有得到正确的值来分隔二进制文件中的数字,但sizeof(float)sizeof(int) 在 Windows 和 Linux 下都有相同的值(4)。

对问题出在哪里有任何想法吗?

感谢您的帮助,

【问题讨论】:

  • 您没有错误捕获。您是否调试并测试了打开文件的错误?
  • 你能展示一个正在读取的输入文件的例子吗?

标签: c++ linux windows ifstream


【解决方案1】:

尝试打开文件进行二进制读取:

ifstream fin(FilenamePLY.c_str(), std::ios::binary );

【讨论】:

    【解决方案2】:

    以二进制模式打开文件并验证您是否打开了文件。

    您当前也在将 sizeof(float) 的数据读入一个字符 - 它会在该点覆盖其他数据 (numFloat, numInt)

    【讨论】:

      猜你喜欢
      • 2014-07-18
      • 1970-01-01
      • 2017-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-10
      • 2011-09-02
      • 2018-07-06
      相关资源
      最近更新 更多