-1

I've been building a program that converts one model file type to a wavefront obj one, but I've ran into a problem when writing my faces to that file.

    if(!strcmp(line , "TEX:TOP"))
    {
        i++;
            TEX_TOP << "f " << i << "/" << i << "/" << i << " ";
        i++;
            TEX_TOP << TEX_TOP << i << "/" << i << "/" << i << " ";
        i++;
            TEX_TOP << TEX_TOP << i << "/" << i << "/" << i << " ";
        i++;
            TEX_TOP << TEX_TOP << i << "/" << i << "/" << i << "\n";
    }

This part of the code is supposed to output..

f 1/1/1 2/2/2 3/3/3 4/4/4

but comes out as..

f 1971327331/1971327331/1971327331 0x28f5a81971327332/1971327332/1971327332 0x28f5a81971327333/1971327333/1971327333 0x28f5a81971327334/1971327334/1971327334

I've searched for hours and still cannot find a fix.

EDIT: Thanks to LihO, my problem was I was placing the TEX_TOP fstream object at the beginning of each line assuming I was re-declaring it, and the int had to start at 0 in order to count up.

    i = 0;

    if(!strcmp(line , "TEX:TOP"))
    {
        i++;
            TEX_TOP << i << "/" << i << "/" << i << " ";
        i++;
            TEX_TOP << i << "/" << i << "/" << i << " ";
        i++;
            TEX_TOP << i << "/" << i << "/" << i << " ";
        i++;
            TEX_TOP << i << "/" << i << "/" << i << "\n";
    }
siba
  • 43
  • 1
  • 7
  • You've left out a lot of important code, including the declaration and initialization of `i` and the definition of `TEX_TOP`. – Carey Gregory Oct 11 '13 at 01:17
  • Those are still `int`s and should not be confused with the `long` data type. – nhgrif Oct 11 '13 at 01:17
  • Well I figured it was fine to leave those parts out since I was looking specifically for the int variable in stringstream. – siba Oct 11 '13 at 01:21

1 Answers1

2

I assume that you are using an uninitialized variable which results in undefined behavior:

int i;

try to explictly zero-initialize it before you start incrementing its value:

int i = 0;

Also note that you are passing the fstream object itself to its operator<<:

TEX_TOP << TEX_TOP << i << "/" << i << "/" << i << " ";

which causes additional unwanted stuff appear in your file. Just change it to:

TEX_TOP << i << "/" << i << "/" << i << " ";
LihO
  • 41,190
  • 11
  • 99
  • 167
  • That definitely fixed the int, but I seem to have some... memory.. pointers?, if I'm remembering correctly what they are. stuff like f 1/1/1 0x28f5a82/2/2 0x28f5a83/3/3 0x28f5a84/4/4 – siba Oct 11 '13 at 01:18
  • The files write their 'steps', different parts of the model information at different parts of the file, so I need to loop through everything, apply them to variables and then write them after all the loops have finished. – siba Oct 11 '13 at 01:20
  • @siba: See my answer now (under line) – LihO Oct 11 '13 at 01:21