views:

139

answers:

1

I have an array like this:

    int[,] iMap = new int[iMapHeight, iMapWidth] { 
                 { 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
                 { 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                 { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                 { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                 { 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                 { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                 { 2, 2, 2, 2, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
                 { 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                 { 0, 0, 0, 1, 1, 1, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                 { 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                 { 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                 { 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                 { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                 { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0},
                 { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                 { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                 { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                 { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                 { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0},
                 { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    };

And I want the same thing coming from a data file. The file should be structured like this preferably:

0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000

Which will ultimately make an array like above with all values set to 0.

What would be the best way to do this? Would I read one line from the file and then split each characters separately and transfer it to the new array? Or perhaps read a line, and then read each character and put that in the array?

Thanks for any help.

+1  A: 

This should work, assuming that the array has a fixed size :

int[,] iMap = new int[iMapHeight, iMapWidth];
using (var reader = new StreamReader(fileName))
{
    for (int i = 0; i < iMapHeight; i++)
    {
        string line = reader.ReadLine();
        for(int j = 0; j < iMapWidth; j++)
        {
            iMap[i, j] = (int)(line[j] - '0');
        }
    }
}
Thomas Levesque
I get an 'index out of range' exception when I use your code. It looks like it'd work but I'm looking through it now trying to figure it out. Especially the `(int)(line[j] - '0');` bit, Im not sure where you split the characters in the line to be put into separate array entries. But thank you very much :)
Joshua
`line` is a line ; `line[j]` is the character at position `j` ; `line[j] - '0'` is the integer represented by that character (i.e. '0' => 0, '1' => 1...)
Thomas Levesque
Regarding the IndexOutOfRangeException, check that the number of lines in the file and the length of the lines are correct
Thomas Levesque
OK, got it... the condition in the second loop was wrong. I fixed it, it works now
Thomas Levesque
Should of noticed that myself :) Thanks. Only problem is now I get this error: `Object reference not set to an instance of an object.` on the line: `iMap[i, j] = (int)(line[j] - '0');`.
Joshua
perhqps because `line` is null... check that your file has enough lines
Thomas Levesque