使用 ifstream 传递字符串时出错?

Error with passing string using ifstream?

提问人:nickn17 提问时间:3/7/2023 最后编辑:genpfaultnickn17 更新时间:3/9/2023 访问量:59

问:

我正在尝试使用程序来加载 obj 文件并显示它。运行程序时,我得到 else 语句打印,因为它无法打开文件,有什么想法吗?

以下是打开和解析部分的代码:

int Model_OBJ::Load(char* filename)
{
    string line;
    ifstream objFile (filename);
    if (objFile.is_open())                                                    // If obj file is open, continue
    {
        objFile.seekg (0, ios::end);                                        // Go to end of the file,
        long fileSize = objFile.tellg();                                    // get file size
        objFile.seekg (0, ios::beg);                                        // we'll use this to register memory for our 3d model
 
        vertexBuffer = (float*) malloc (fileSize);                            // Allocate memory for the verteces
        Faces_Triangles = (float*) malloc(fileSize*sizeof(float));            // Allocate memory for the triangles
        normals  = (float*) malloc(fileSize*sizeof(float));                    // Allocate memory for the normals
 
        int triangle_index = 0;                                                // Set triangle index to zero
        int normal_index = 0;                                                // Set normal index to zero
 
        while (! objFile.eof() )                                            // Start reading file data
        {
            getline (objFile,line);                                            // Get line from file
 
            if (line.c_str()[0] == 'v')                                        // The first character is a v: on this line is a vertex stored.
            {
                line[0] = ' ';                                                // Set first character to 0. This will allow us to use sscanf
 
                sscanf(line.c_str(),"%f %f %f ",                            // Read floats from the line: v X Y Z
                    &vertexBuffer[TotalConnectedPoints],
                    &vertexBuffer[TotalConnectedPoints+1],
                    &vertexBuffer[TotalConnectedPoints+2]);
 
                TotalConnectedPoints += POINTS_PER_VERTEX;                    // Add 3 to the total connected points
            }
            if (line.c_str()[0] == 'f')                                        // The first character is an 'f': on this line is a point stored
            {
                line[0] = ' ';                                                // Set first character to 0. This will allow us to use sscanf
 
                int vertexNumber[4] = { 0, 0, 0 };
                sscanf(line.c_str(),"%i%i%i",                                // Read integers from the line:  f 1 2 3
                    &vertexNumber[0],                                        // First point of our triangle. This is an
                    &vertexNumber[1],                                        // pointer to our vertexBuffer list
                    &vertexNumber[2] );                                        // each point represents an X,Y,Z.
 
                vertexNumber[0] -= 1;                                        // OBJ file starts counting from 1
                vertexNumber[1] -= 1;                                        // OBJ file starts counting from 1
                vertexNumber[2] -= 1;                                        // OBJ file starts counting from 1
 
 
                /********************************************************************
                 * Create triangles (f 1 2 3) from points: (v X Y Z) (v X Y Z) (v X Y Z).
                 * The vertexBuffer contains all verteces
                 * The triangles will be created using the verteces we read previously
                 */
 
                int tCounter = 0;
                for (int i = 0; i < POINTS_PER_VERTEX; i++)
                {
                    Faces_Triangles[triangle_index + tCounter   ] = vertexBuffer[3*vertexNumber[i] ];
                    Faces_Triangles[triangle_index + tCounter +1 ] = vertexBuffer[3*vertexNumber[i]+1 ];
                    Faces_Triangles[triangle_index + tCounter +2 ] = vertexBuffer[3*vertexNumber[i]+2 ];
                    tCounter += POINTS_PER_VERTEX;
                }
 
                /*********************************************************************
                 * Calculate all normals, used for lighting
                 */
                float coord1[3] = { Faces_Triangles[triangle_index], Faces_Triangles[triangle_index+1],Faces_Triangles[triangle_index+2]};
                float coord2[3] = {Faces_Triangles[triangle_index+3],Faces_Triangles[triangle_index+4],Faces_Triangles[triangle_index+5]};
                float coord3[3] = {Faces_Triangles[triangle_index+6],Faces_Triangles[triangle_index+7],Faces_Triangles[triangle_index+8]};
                float *norm = this->calculateNormal( coord1, coord2, coord3 );
 
                tCounter = 0;
                for (int i = 0; i < POINTS_PER_VERTEX; i++)
                {
                    normals[normal_index + tCounter ] = norm[0];
                    normals[normal_index + tCounter +1] = norm[1];
                    normals[normal_index + tCounter +2] = norm[2];
                    tCounter += POINTS_PER_VERTEX;
                }
 
                triangle_index += TOTAL_FLOATS_IN_TRIANGLE;
                normal_index += TOTAL_FLOATS_IN_TRIANGLE;
                TotalConnectedTriangles += TOTAL_FLOATS_IN_TRIANGLE;
            }
        }
        objFile.close();                                                        // Close OBJ file
    }
    else
    {
        cout << "Unable to open file";
    }
    return 0;
}

这是我调用它的方式:main()

obj.Load("cow.obj");

我尝试将文件的目录而不是文件名放入项目主管中,但两者都不起作用。

C++ IOSTREAM 工作目录 波前

评论

0赞 Yksisarvinen 3/7/2023
位于何处以及从哪里启动程序?cow.obj
4赞 Eljay 3/7/2023
这里有很多代码,远远超出了最小示例所需的代码。这里也没有足够的代码来提供完整的示例。最小可重复的例子。帮助我们帮助您。此外,虽然 eof 可能是一个问题。
2赞 463035818_is_not_an_ai 3/7/2023
你为什么不使用?std::string
4赞 MikeCAT 3/7/2023
请注意,您的用法是错误的while (! objFile.eof() )
4赞 PaulMcKenzie 3/7/2023
它无法打开文件,有什么想法吗?-- 让我们访问您的机器,我们将能够告诉您原因。否则,我们怎么知道为什么您的文件无法打开?也许没有找到?可能您没有打开该文件的权限?另外,如果问题本质上是为什么不打开文件,为什么还要有所有这些代码?ifstream objFile("cow.obj")

答: 暂无答案