strange issue with input from a file

Mar 31, 2010 at 9:10am
Alright, I've been confounded by this problem, and I honestly don't understand whats causing it.

Basically what I'm trying to do is load a file so I can read data from it using a string previously found from another file.

Heres my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
void LoadOBJ(char filename[1024]){

...

string mtldirectory;

...

//now we can load our mtl file

    string mtlline;
    ifstream mtlFile;

cout << "objects/tower/tower.mtl" << endl << mtldirectory;

    mtlFile.open(mtldirectory.c_str());
    if( !mtlFile.is_open() )  {cout << "\nError opening MTL file";}
    else {
        while (mtlFile.good()){
            getline(mtlFile, mtlline, '\n');
                if ( mtlline.substr(0,6) == "newmtl"){
                    mtlline.erase (0,7);
                    mtl_texture_data[0][curr_texture] = mtlline;
                    curr_texture++;

                }
                if ( mtlline.substr(0,6) == "map_Kd"){
                    mtlline.erase (0,7);
                    mtl_texture_data[1][curr_texture] = mtlline;

                }
        }
        mtlFile.close();
        for(int i = 0; i < 4; i++){
            obj_textures[0] = LoadTexture(mtl_texture_data[1][i]);
            last_texture++;}
    }
}


my output is:

objects/tower/tower.mtl
objects/tower/tower.mtl
Error opening MTL file


The real kicker is that when I replace: mtlFile.open(mtldirectory.c_str()); with:
1
2
char mtlfilename[1024] = {"objects/tower/tower.mtl"};
mtlFile.open(mtlfilename);
It works...
Mar 31, 2010 at 9:57am
The real kicker is that when I replace: mtlFile.open(mtldirectory.c_str()); with:


Woah, that sure sound strange.

Hm, did you try to make the "most simple case where it still fails"? Try to remove everything step by step until it suddenly works (or your program is empty. If it still fails, sue your compiler vendor *g*). IF it's really like your kicker here, your implementation of stdlib is broken. But maybe it's just something stupid like you forgot to close the file somewhere and there's locking issues and when you tried to test it you did something other different or so..


Ciao, Imi.
PS: And post if you got news. You got my interest! ;-)
Mar 31, 2010 at 10:33am
Hm, did you try to make the "most simple case where it still fails"?


Not to the extent that I need to, I'm just about to pick everything apart and I'll reply if i figure it out. At this point since the code looks correct, I think its something interfering. Also I hope it's not a broken lib since I'm on a clean install of Ubuntu xD

Also I just tried forcibly setting mtldirectory as mtldirectory = "objects/tower/tower.mtl"; and it gives me the output:
objects/tower/tower.mtl
Segmentation fault


Which actually gives me something to look for :D!
Mar 31, 2010 at 10:38am
objects/tower/tower.mtl
Segmentation fault


hm.. if this code below gives you a segfault, then something is REALLY broken.

1
2
3
4
5
6
7
#include <string>
int main()
{
    std::string s;
    s = "objects/tower/tower.mtl";
    std::cout << s.c_str() << std::endl;
}


So if you even can't get this to work, you really should reinstall some stuff there.

Ciao, Imi.
Topic archived. No new replies allowed.