-1

I am trying to read in the following OBJ file

#--- ObjWriter ---
v -0.599972 -0.599972 -0.599972
v -0.304591 -0.834531 0.539150
.
.
.
f 1 2 6 
f 1 6 5 
f 1 5 7 
.
.
.

I am trying to set up a function that will store the values of a vertex (3 values following the char v) to a vector of points (Pt being made up of a x, y, and z) and the values of a face (3 values following the char f) to another vector of faces (Face being made up of 3 ints). So far, I have attempted this with the following function:

void readFile(char *inFile)
{
    ifstream inF(inFile);
    string line;

    while (getline(inF, line))
    {
        if (line[0] == 'v')
        {
            float x, y, z;
            inF >> x >> y >> z;

            // cout << x;

            verts.push_back(Pt(x, y, z));
        }
        else if (line[0] == 'f')
        {
            int x, y, z;
            inF >> x >> y >> z;
            faces.push_back(Face(x, y, z));
        }
        else if (line[0] == '#')
            continue;
    }
}

Whenever I test this method by displaying the x value in the first if statement, I get the value "-858993460". How can I fix this function? Placement of the cout line is shown commented out.

JMV12
  • 965
  • 1
  • 20
  • 52
  • `-858993460` is `0xCCCCCCCC` in hex. It's the pattern the debug version of the CRT that ships with Visual Studio assigns to uninitialized data. Since we cannot how you *"display the x value in the first if statement"*, we cannot help you, or provide an explanation. – IInspectable Nov 02 '16 at 22:10
  • I placed the cout where I did to test it. Also, what would be the best way to test that all the values have been successfully stored? – JMV12 Nov 02 '16 at 22:18
  • The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Nov 02 '16 at 22:21

1 Answers1

0

instead of writing your own obj file reader you could for example use assimp (http://www.assimp.org)

regarding your code: you should check if line is empty before accessing the string at index 0. Then, when reading the float values, you must first skip char from the line ("v" in the case of a vertex). You could for example just read it into a dummy char variable. And finally, >> changes the state of the input stream. You could for example use a stringstream and initialize it with the current line and then read from this stream.

Something like this:

if(line.empty())
    continue;

if (line[0] == 'v')
{
    float x, y, z;
    char dummy;
    std::stringstream ss(line);
    ss >> dummy >> x >> y >> z;
    cout << x << ";" << y << ";" << z << "\n";
}
Harry
  • 1,105
  • 8
  • 20
  • Why read the first character into a dummy character, instead of reading it into a proper `char` variable, and using that as the input for a `switch`/`case` statement? – IInspectable Nov 02 '16 at 22:32
  • Checking the char allows me to save either the floats for v or ints for f to use in future calculations. I need to differentiate between sets of values. – JMV12 Nov 02 '16 at 22:41
  • I wanted to show how to get the code running without too much changes. If I would implement this I wouldn't use stringstreams and that stuff and would implement a simple parser (FSM) and therefore would also branch on the first char(s) of the line. – Harry Nov 02 '16 at 22:42