Im reading a text file and storing the characters in an array. I get a "Unhandled exception at 0x0109985e in Executable.exe: 0xC0000005: Access violation writing location 0xcccccccc."

void drawMap(string MapLoc) {
    int i = 0;
    int x = 0;
    int y = 0;
    char* arrayMap[5225];
    ifstream mapReader;
    mapReader.open(MapLoc);

    for (i = 0; i <= 5225; i++) 
    {
        while (mapReader.good())
        {
*           mapReader >> arrayMap[i];
        }
    }
    mapReader.close();

    for (i = 0; i = 5225; i--)
    {
        if ((i = 96) || (i = 191) || (i = 286) || (i = 381) || (i = 476) || (i = 571) || (i = 666) || (i = 761) || (i = 856) || (i = 951) || (i = 1046) || (i = 1141)
            || (i = 1236) || (i = 1331) || (i = 1426) || (i = 1521) || (i = 1616) || (i = 1711) || (i = 1806) || (i = 1901) || (i = 1996) || (i = 2091) || (i = 2186) || (i = 2281)
            || (i = 2376) || (i = 2471) || (i = 2566) || (i = 2661) || (i = 2756) || (i = 2851) || (i = 2946) || (i = 3041) || (i = 3136) || (i = 3231) || (i = 3326) || (i = 3421)
            || (i = 3516) || (i = 3611) || (i = 3706) || (i = 3801) || (i = 3896) || (i = 3991) || (i = 4086) || (i = 4181) || (i = 4276) || (i = 4371) || (i = 4466) || (i = 4561)
            || (i = 4656) || (i = 4751) || (i = 4846) || (i = 4941) || (i = 5036) || (i = 5131))
        {
            y++;
        }
        if (arrayMap[i] == "X")
                {
                    //Draw Wall
                    glColor4ub(255,255,255,255);
                    glBegin(GL_QUADS);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glEnd();
                }
                else if (arrayMap[i] == ".")
                {
                    //Draw Space
                    glColor4ub(0,0,0,255);
                    glBegin(GL_QUADS);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glEnd();
                }
                else if (arrayMap[i] == "C")
                {
                    //Draw Char
                }
                else if (arrayMap[i] == "O")
                {
                    //Draw Opponent
                }
    }
}

Happens when executing the code at the *

mapReader >> arrayMap[i];

Except for the typo or whatever

for (i = 0; i = 5225; i--) // ???

try initializing the array with less than 1024 and see if you get the same problem.

(edit) At the second thought (I missed that part when I posted the previous message), you have loop in loop:

    for (i = 0; i <= 5225; i++)
    {
        while (mapReader.good())
        {
            mapReader >> arrayMap[i];
        }
    }

which should be one of:

    for (i = 0; i <= 5225; i++)
    {
        if (mapReader.good())
        {
            mapReader >> arrayMap[i];
        }
    }

or

    int i = 0;
    while (mapReader.good())
    {
        mapReader >> arrayMap[i];
        i++;
    }

That's because while loop traverses the file.

Even with the loop replaced and the array value lowered, it still gives an access violation error.

    *_Str++ = _Traits::to_char_type(_Meta); // add it to string

Thats where it breaks at in istream.

char* arrayMap[5225];

So the max number you want to test or alter is 5224, arrays start at 0.

for (i = 0; i < 5225; i++)  // <===================
    {
        if (mapReader.good())
        {
            mapReader >> arrayMap[i];
        }
    }

You need to make sure your incrementer does not exceed that number.
Also, not knowing what mapReader does, it couls be that you are writing incorrect data to arrayMap, which is char** (a pointer to a pointer).

Im reading in characters from a file, which is read through from MapLoc. Only specific characters, and I know the file doesnt contain a value not known. (X, ., C, and O are the characters)

Then when theyre being read, the characters will individually go into an array 'arrayMap' where they are then read and processed accordingly here:

if (arrayMap[i] == "X")
                {
                    //Draw Wall
                    glColor4ub(255,255,255,255);
                    glBegin(GL_QUADS);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glEnd();
                }
                else if (arrayMap[i] == ".")
                {
                    //Draw Space
                    glColor4ub(0,0,0,255);
                    glBegin(GL_QUADS);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glEnd();
                }
                else if (arrayMap[i] == "C")
                {
                    //Draw Char
                }
                else if (arrayMap[i] == "O")
                {
                    //Draw Opponent
                }

The error happens when reading from the file, after opening.

arrayMap[i] will never == "X" because arrayMap[i] is a pointer to char*

try declaring it like this char arrayMap[5225];

operand types are incompatible ("char" and "const char *")

error given at

if (arrayMap[i] == "X") // <---
                {
                    //Draw Wall
                    glColor4ub(255,255,255,255);
                    glBegin(GL_QUADS);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glEnd();
                }
                else if (arrayMap[i] == ".") // <---
                {
                    //Draw Space
                    glColor4ub(0,0,0,255);
                    glBegin(GL_QUADS);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glEnd();
                }
                else if (arrayMap[i] == "C") // <---
                {
                    //Draw Char
                }
                else if (arrayMap[i] == "O") // <---
                {
                    //Draw Opponent
                }

operand types are incompatible ("char" and "const char *")

You should use the standard C++ string class. C-style strings are awkward, low level, and easy to get wrong (as evidenced by this thread). However, let's look at the problems in your original code, and ignore all of the wrong advice you've been given up to this point.

This is wrong:

char* arrayMap[5225];
...
mapReader >> arrayMap[i];

Why is it wrong? Because arrayMap[i] doesn't point to memory that you own; you're extremely likely to get a memory access violation at runtime. Memory must be allocated first, but doing that would be awkward, low level, and easy to get wrong (as evidenced by this thread). Yes, I'm intentionally repeating myself.

This is wrong too:

if (arrayMap[i] == "X")

The equality operator doesn't work the way you think it does at a lower level. arrayMap[i] is a pointer, and "X" evaluates to a pointer. Unless the planets align and you get a perfect storm of shared string literals in the compiler implementation and arrayMap[i] was conveniently initialized to "X", this test will always fail.

To compare C-style strings you must compare all characters up to a null character ('\0'). The simplest way to do that is by calling the std::strcmp() function:

if (std::strcmp(arrayMap[i], "X") == 0)

Or, since these are single character tests, you can just look at the first character:

if (arrayMap[i][0] == 'X')

But you can greatly simplify things by using C++ strings instead. One simple change will fix all of your string handling problems in that function:

string arrayMap[5225];

Now that that's taken care of, you really need to remember the difference between the = operator and the == operator. The former is assignment and the latter is comparison. You're mixing and matching them, and it's going to bork up your code big time.

I'd probably make a few assumptions and fix your code like so:

void drawMap(string MapLoc)
{
    string arrayMap[5225];
    int x = 0, y = 0;
    int n = 0;

    {
        ifstream mapReader(MapLoc);

        while (n < 5225)
        {
            if (!(mapReader >> arrayMap[n++]))
                break;
        }
    }

    for (int i = 0; i < n; i++)
    {
        if ((i == 96) || (i == 191) || (i == 286) || (i == 381) || (i == 476) || (i == 571) || (i == 666) || (i == 761) || 
            (i == 856) || (i == 951) || (i == 1046) || (i == 1141) ||(i == 1236) || (i == 1331) || (i == 1426) || (i == 1521) || 
            (i == 1616) || (i == 1711) || (i == 1806) || (i == 1901) || (i == 1996) || (i == 2091) || (i == 2186) || (i == 2281) ||
            (i == 2376) || (i == 2471) || (i == 2566) || (i == 2661) || (i == 2756) || (i == 2851) || (i == 2946) || (i == 3041) || 
            (i == 3136) || (i == 3231) || (i == 3326) || (i == 3421) || (i == 3516) || (i == 3611) || (i == 3706) || (i == 3801) || 
            (i == 3896) || (i == 3991) || (i == 4086) || (i == 4181) || (i == 4276) || (i == 4371) || (i == 4466) || (i == 4561) ||
            (i == 4656) || (i == 4751) || (i == 4846) || (i == 4941) || (i == 5036) || (i == 5131))
        {
            y++;
        }

        if (arrayMap[i] == "X")
        {
            // Draw Wall
            glColor4ub(255, 255, 255, 255);
            glBegin(GL_QUADS);
            glVertex2f(10 * x, 10 * y);
            glVertex2f(10 * x, 10 * y);
            glVertex2f(10 * x, 10 * y);
            glVertex2f(10 * x, 10 * y);
            glEnd();
        }
        else if (arrayMap[i] == ".")
        {
            // Draw Space
            glColor4ub(0, 0, 0, 255);
            glBegin(GL_QUADS);
            glVertex2f(10 * x, 10 * y);
            glVertex2f(10 * x, 10 * y);
            glVertex2f(10 * x, 10 * y);
            glVertex2f(10 * x, 10 * y);
            glEnd();
        }
        else if (arrayMap[i] == "C")
        {
            // Draw Char
        }
        else if (arrayMap[i] == "O")
        {
            // Draw Opponent
        }
    }
}

One of the assumptions is that the algorithm in general is correct and what you want.

operand types are incompatible ("char" and "const char *")

To test a char you need single quotes

if (arrayMap[i] == 'X')

Also, all the advice in this thread is not wrong, and there is nothing wrong with using c style strings if you wish to.

Also, all the advice in this thread is not wrong

Your most recent post is factually correct (but potentially problematic depending on the format of the file). My previous post is factually correct. Everything prior to that is either flat out wrong or incomplete to the point of being wrong. You can choose which one applies to each post.

If you can't see the wrongness, please consider that you might not be in the best position to help others, and don't be afraid to defer to those who know what they're talking about.

and there is nothing wrong with using c style strings if you wish to.

I never said it was wrong. I said it was awkward, low level, and easy to mess up. If you wish to make things harder on yourself by using a more advanced approach when you lack the experience to do it right, that's your own business. But it would be negligent of me to not mention clearly superior alternatives.

I do not mean to offend you, perhaps you could just point out to me what is wrong with anything in any post I made.

Hate to but in but to answer your question Suzie999 your first post neglected the fact that mapReader is a stream object and he is trying to put information from the stream into an uninitialized pointer array. Your second post wouldn’t work either since he is trying to read in an array of strings. Your suggestion is to put the file into one string which could cause problems and is not what the OP is trying to accomplish. I wouldn’t say that your advice was wrong but IMHO it wasn’t correct.

Fair enough.
Thanks.

Im not sure, but I dont believe deceptikon's code is correct, I need to check if the current array index value is equal to one of numbers listed so that i can make it start drawing on the next line.

Aswell its not drawing, which means either its not reading the file correct, or its not reading array correct.

By the way, the array is 95x55=5225, im basically making an grid.

So you want a two dimentional array of char, or are you using strings?

If you want an array of char like a grid, you should have it like so.

arrayMap[95][55];

here is example

char arrayMap[3][3];

    for (int y = 0; y < 3; y++){

        for (int x = 0; x < 3; x++){

            arrayMap[x][y] = 'z';
        }

    }

In your minds eye, you might view that array as so...

zzz
zzz
zzz

3 rows and 3 columns.

I know, I tried that at first although it didnt work.

void drawMap(std::string MapLoc) {
    char* gridMap[95] [55];
    ifstream Map(MapLoc, ifstream::in, ifstream::binary);
    while (Map.good())
    {
    for(int i = 0; i < 95; i++)
        {
        for(int j = 0; j < 55; j++)
            {
                if (gridMap[i] [j] == "X")
                {
                    //Draw Wall
                    glColor4ub(255,255,255,255);
                    glBegin(GL_QUADS);
                    glVertex2f(10*i,10*j);
                    glVertex2f(10*i,10*j);
                    glVertex2f(10*i,10*j);
                    glVertex2f(10*i,10*j);
                    glEnd();
                }
                else if (gridMap[i] [j] == ".")
                {
                    //Draw Space
                    glColor4ub(0,0,0,255);
                    glBegin(GL_QUADS);
                    glVertex2f(10*i,10*j);
                    glVertex2f(10*i,10*j);
                    glVertex2f(10*i,10*j);
                    glVertex2f(10*i,10*j);
                    glEnd();
                }
                else if (gridMap[i] [j] == "C")
                {
                    //Draw Char
                }
                else if (gridMap[i] [j] == "O")
                {
                    //Draw Opponent
                }
            }
        }
    }
    Map.close();
}

Thats basically what I had.

Did you try gridMap[i] [j] == 'X' (single quotes) ?

I honestly just think were backtracking now. I just think we should stick with the code given and go from there. Unless you figure out all the code to get it working, then I dont believe we should be using another whole new set of code.

I think we're not backtracking, it is a very simple question which you seem to not want to answer, "X" is not the same as 'X'.

Anyway, I'm out, good luck.

Sorry if it seems that way, I lost my code snippet from that, that contains the actuall reading of the file, and I cant seem to figure out how I did it before. Aswell, I believe it would be easier the way Im currently doing it. Please tell me if I am wrong.

Sorry if it seems that way, I lost my code snippet from that, that contains the actuall reading of the file, and I cant seem to figure out how I did it before. Aswell, I believe it would be easier the way Im currently doing it. Please tell me if I am wrong.

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.