为什么这段代码无法读取Blender的.obj文件?

时间:2014-08-22 12:34:34

标签: c++ iostream wavefront

您好我正在尝试阅读使用Blender创建的Wavefront文件。我将此文件的副本放入解决方案资源管理器中。当我第一次尝试编译时,我收到以下消息:

  

致命错误LNK1107:文件无效或损坏:无法读取0x ...

似乎编译器将Blender的.obj文件与其他使用.obj结尾的格式混淆了。解决方案是在其属性中从构建过程中排除该文件。

现在应用程序确实编译了,但没有像我期望的那样显示数据。不确定这是否是代码问题。

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string>
using namespace std;

void ReadPrintFile(string _fileName)
{
    std::string line;
    std::ifstream fileStream (_fileName);   

    if (fileStream.is_open())
    {
        while (getline(fileStream,line))
        {
            cout << line << '\n';
        }
        fileStream.close();
    }
    else
    {
        cout << "Unable to read file";
    }
}

int _tmain(int argc, _TCHAR* argv[])
{

    ReadPrintFile("Drone.obj");
    std::cin.get();

    return 0;
}

代码不会跳转到else语句。文件流似乎只是空的,我直接转发到cin.get();语句。我知道有很多关于如何用C ++解析.OBJ的教程,但我想理解。

1 个答案:

答案 0 :(得分:2)

诀窍不是将文件复制到解决方案资源管理器中,而是复制到项目文件夹中。