I can't seem to figure this out. The multidimensional array "structure" changes its values without any reason.

I am using Visual Studio 2010 for debugging, and when it enters "structure[x][y] = readData" for the first time, it changes the array's value, but it changes back on next line. I don't understand why "mapFile.getline(readData, 10, ',')" alters the array "stucture".

By the way, the file that is being read (fName) is a txt-file containing "1,0,row,1,1,end".

void graphicsLoader::loadFromFile(std::string fName) {  
    //x and y position for tile
    int x;
    int y;
    char readData [9] = "";
    //structure[x][y]
    char *structure[100][100];
    //open file for reading
    std::fstream mapFile(fName, std::fstream::in);
    //read first entity from file
    mapFile.getline(readData, 10, ',');
    for (y = 1; y <= 100; y++){      
        for (x = 1; x <= 100; x++){          
            if (strcmp(readData, "row") == false || strcmp(readData, "end") == false){
                //New row begins. Reset x pos. and advance y pos.
                x = 0;
                break;
            }
            structure[x][y] = readData;
            mapFile.getline(readData, 10, ',');
        }
        mapFile.getline(readData, 10, ',');
    }   
    mapFile.close();
}

Recommended Answers

All 5 Replies

By the way, it was something wrong when I posted this, so had to put all the text in a code block.

By the way, it was something wrong when I posted this, so had to put all the text in a code block.

Our code validation algorithm will very likely prevent you from posting when you format your text like a letter or formal prose (ie. leading paragraph indentation) because leading whitespace is how our formatting language recognizes and highlights actual code.

You can (and should!) avoid this by starting all non-code text lines at column zero.

p.s. I fixed your first post. ;)

The problem is a conceptual misunderstanding about pointers, but ultimately it boils down to this:

structure[x][y] = readData;

All of the pointers in structure are pointing to the same array, and that array is constantly changing because you use it as the temporary buffer for input. What you really want are copies of readData, not references to readData. The easiest way to do that would be make structure an array of std::string, not pointers to char:

std::string structure[100][100];

readData should also be a std::string so that you don't have to worry about buffer overflow. It would immediately solve the current bug in your code where getline()'s limit exceeds the size of the array (ie. it should be 9, or sizeof(readData)).

Thanks for your help. I guess I didn't think that far, but of course it makes sense. Now that I use string instead of a char pointer, I'm having trouble seeing the individual strings in the watch window when I'm debugging my code. Is there any reason for that? When I expand "structure"->"[1]" I only get "[size]" and "[capacity]" instead of the other dimension of the array.

I'm having trouble seeing the individual strings in the watch window when I'm debugging my code. Is there any reason for that?

The debugger probably isn't expanding dynamic strings, which is what std::string uses internally. So what you're seeing are the concrete data members. The actual data of the string is probably represented as an address (like 0x12345678), and you might be able to expand that as well to see the string contents. It really depends on the debugger in question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.