C# acting weird when reading in values from a file to an array
This is the structure of my file:
1111111111111111111111111
2222222222222222222222222
3333333333333333333333333
4444444444444444444444444
5555555555555555555555555
6666666666666666666666666
7777777777777777777777777
8888888888888888888888888
9999999999999999999999999
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
And this is the code I'm using to read it into an array:
using (StreamReader reader = new StreamReader(mapPath))
{
string line;
for (int i = 0; i < iMapHeight; i++)
{
if ((line = reader.ReadLine()) != null)
{
for (int j = 0; j < iMapWidth; j++)
{
iMap[i, j] = line[j];
}
}
}
}
I have done some debugging, and line[j]
correctly iterates through each character in the currently read line. The problem lies with iMap[i, j]
. After this block of code executes, this is the contents of iMap
:
- iMap {int[14, 25]} int[,]
[0, 0] 49 int
[0, 1] 49 int
[0, 2] 49 int
[0, 3] 49 int
[0, 4] 49 int
[0, 5] 49 int
[0, 6] 49 int
[0, 7] 49 int
[0, 8] 49 int
[0, 9] 49 int
[0, 10] 49 int
[0, 11] 49 int
[0, 12] 49 int
[0, 13] 49 int
[0, 14] 49 int
[0, 15] 49 int
[0, 16] 49 int
[0, 17] 49 int
[0, 18] 49 int
[0, 19] 49 int
[0, 20] 49 int
[0, 21] 49 int
[0, 22] 49 int
[0, 23] 49 int
[0, 24] 49 int
[1, 0] 50 int
[1, 1] 50 int
[1, 2] 50 int
[1, 3] 50 int
[1, 4] 50 int
[1, 5] 50 int
[1, 6] 50 int
[1, 7] 50 int
[1, 8] 50 int
[1, 9] 50 int
[1, 10] 50 int
[1, 11] 50 int
[1, 12] 50 int
[1, 13] 50 int
[1, 14] 50 int
[1, 15] 50 int
[1, 16] 50 int
[1, 17] 50 int
[1, 18] 50 int
[1, 19] 50 int
[1, 20] 50 int
[1, 21] 50 int
[1, 22] 50 int
[1, 23] 50 int
[1, 24] 50 int
[2, 0] 51 int
[2, 1] 51 int
[2, 2] 51 int
[2, 3] 51 int
[2, 4] 51 int
[2, 5] 51 int
[2, 6] 51 int
[2, 7] 51 int
[2, 8] 51 int
[2, 9] 51 int
[2, 10] 51 int
[2, 11] 51 int
[2, 12] 51 int
[2, 13] 51 int
[2, 14] 51 int
[2, 15] 51 int
[2, 16] 51 int
[2, 17] 51 int
[2, 18] 51 int
[2, 19] 51 int
[2, 20] 51 int
[2, 21] 51 int
[2, 22] 51 int
[2, 23] 51 int
[2, 24] 51 int
[3, 0] 52 int
[3, 1] 52 int
[3, 2] 52 int
[3, 3] 52 int
[3, 4] 52 int
[3, 5] 52 int
[3, 6] 52 int
[3, 7] 52 int
[3, 8] 52 int
[3, 9] 52 int
[3, 10] 52 int
[3, 11] 52 int
[3, 12] 52 int
[3, 13] 52 int
[3, 14] 52 int
[3, 15] 52 int
[3, 16] 52 int
[3, 17] 52 int
[3, 18] 52 int
[3, 19] 52 int
[3, 20] 52 int
[3, 21] 52 int
[3, 22] 52 int
[3, 23] 52 int
[3, 24] 52 int
[4, 0] 53 int
[4, 1] 53 int
[4, 2] 53 int
[4, 3] 53 int
[4, 4] 53 int
[4, 5] 53 int
[4, 6] 53 int
[4, 7] 53 int
[4, 8] 53 int
[4, 9] 53 int
[4, 10] 53 int
[4, 11] 53 int
[4, 12] 53 int
[4, 13] 53 int
[4, 14] 53 int
[4, 15] 53 int
[4, 16] 53 int
[4, 17] 53 int
[4, 18] 53 int
[4, 19] 53 int
[4, 20] 53 int
[4, 21] 53 int
[4, 22] 53 int
[4, 23] 53 int
[4, 24] 53 int
[5, 0] 54 int
[5, 1] 54 int
[5, 2] 54 int
[5, 3] 54 int
[5, 4] 54 int
[5, 5] 54 int
[5, 6] 54 int
[5, 7] 54 int
[5, 8] 54 int
[5, 9] 54 int
[5, 10] 54 int
[5, 11] 54 int
[5, 12] 54 int
[5, 13] 54 int
[5, 14] 54 int
[5, 15] 54 int
[5, 16] 54 int
[5, 17] 54 int
[5, 18] 54 int
[5, 19] 54 int
[5, 20] 54 int
[5, 21] 54 int
[5, 22] 54 int
[5, 23] 54 int
[5, 24] 54 int
[6, 0] 55 int
[6, 1] 55 int
[6, 2] 55 int
[6, 3] 55 int
[6, 4] 55 int
[6, 5] 55 int
[6, 6] 55 int
[6, 7] 55 int
[6, 8] 55 int
开发者_StackOverflow [6, 9] 55 int
[6, 10] 55 int
[6, 11] 55 int
[6, 12] 55 int
[6, 13] 55 int
[6, 14] 55 int
[6, 15] 55 int
[6, 16] 55 int
[6, 17] 55 int
[6, 18] 55 int
[6, 19] 55 int
[6, 20] 55 int
[6, 21] 55 int
[6, 22] 55 int
[6, 23] 55 int
[6, 24] 55 int
[7, 0] 56 int
[7, 1] 56 int
[7, 2] 56 int
[7, 3] 56 int
[7, 4] 56 int
[7, 5] 56 int
[7, 6] 56 int
[7, 7] 56 int
[7, 8] 56 int
[7, 9] 56 int
[7, 10] 56 int
[7, 11] 56 int
[7, 12] 56 int
[7, 13] 56 int
[7, 14] 56 int
[7, 15] 56 int
[7, 16] 56 int
[7, 17] 56 int
[7, 18] 56 int
[7, 19] 56 int
[7, 20] 56 int
[7, 21] 56 int
[7, 22] 56 int
[7, 23] 56 int
[7, 24] 56 int
[8, 0] 57 int
[8, 1] 57 int
[8, 2] 57 int
[8, 3] 57 int
[8, 4] 57 int
[8, 5] 57 int
[8, 6] 57 int
[8, 7] 57 int
[8, 8] 57 int
[8, 9] 57 int
[8, 10] 57 int
[8, 11] 57 int
[8, 12] 57 int
[8, 13] 57 int
[8, 14] 57 int
[8, 15] 57 int
[8, 16] 57 int
[8, 17] 57 int
[8, 18] 57 int
[8, 19] 57 int
[8, 20] 57 int
[8, 21] 57 int
[8, 22] 57 int
[8, 23] 57 int
[8, 24] 57 int
[9, 0] 48 int
[9, 1] 48 int
[9, 2] 48 int
[9, 3] 48 int
[9, 4] 48 int
[9, 5] 48 int
[9, 6] 48 int
[9, 7] 48 int
[9, 8] 48 int
[9, 9] 48 int
[9, 10] 48 int
[9, 11] 48 int
[9, 12] 48 int
[9, 13] 48 int
[9, 14] 48 int
[9, 15] 48 int
[9, 16] 48 int
[9, 17] 48 int
[9, 18] 48 int
[9, 19] 48 int
[9, 20] 48 int
[9, 21] 48 int
[9, 22] 48 int
[9, 23] 48 int
[9, 24] 48 int
[10, 0] 48 int
[10, 1] 48 int
[10, 2] 48 int
[10, 3] 48 int
[10, 4] 48 int
[10, 5] 48 int
[10, 6] 48 int
[10, 7] 48 int
[10, 8] 48 int
[10, 9] 48 int
[10, 10] 48 int
[10, 11] 48 int
[10, 12] 48 int
[10, 13] 48 int
[10, 14] 48 int
[10, 15] 48 int
[10, 16] 48 int
[10, 17] 48 int
[10, 18] 48 int
[10, 19] 48 int
[10, 20] 48 int
[10, 21] 48 int
[10, 22] 48 int
[10, 23] 48 int
[10, 24] 48 int
[11, 0] 48 int
[11, 1] 48 int
[11, 2] 48 int
[11, 3] 48 int
[11, 4] 48 int
[11, 5] 48 int
[11, 6] 48 int
[11, 7] 48 int
[11, 8] 48 int
[11, 9] 48 int
[11, 10] 48 int
[11, 11] 48 int
[11, 12] 48 int
[11, 13] 48 int
[11, 14] 48 int
[11, 15] 48 int
[11, 16] 48 int
[11, 17] 48 int
[11, 18] 48 int
[11, 19] 48 int
[11, 20] 48 int
[11, 21] 48 int
[11, 22] 48 int
[11, 23] 48 int
[11, 24] 48 int
[12, 0] 48 int
[12, 1] 48 int
[12, 2] 48 int
[12, 3] 48 int
[12, 4] 48 int
[12, 5] 48 int
[12, 6] 48 int
[12, 7] 48 int
[12, 8] 48 int
[12, 9] 48 int
[12, 10] 48 int
[12, 11] 48 int
[12, 12] 48 int
[12, 13] 48 int
[12, 14] 48 int
[12, 15] 48 int
[12, 16] 48 int
[12, 17] 48 int
[12, 18] 48 int
[12, 19] 48 int
[12, 20] 48 int
[12, 21] 48 int
[12, 22] 48 int
[12, 23] 48 int
[12, 24] 48 int
[13, 0] 48 int
[13, 1] 48 int
[13, 2] 48 int
[13, 3] 48 int
[13, 4] 48 int
[13, 5] 48 int
[13, 6] 48 int
[13, 7] 48 int
[13, 8] 48 int
[13, 9] 48 int
[13, 10] 48 int
[13, 11] 48 int
[13, 12] 48 int
[13, 13] 48 int
[13, 14] 48 int
[13, 15] 48 int
[13, 16] 48 int
[13, 17] 48 int
[13, 18] 48 int
[13, 19] 48 int
[13, 20] 48 int
[13, 21] 48 int
[13, 22] 48 int
[13, 23] 48 int
[13, 24] 48 int
Sorry for the lame formatting, but it's huge :P I have no idea where it's getting these values from, does anyone have an explanation? Thanks :)
These are the ASCII values of the character '0', '1' etc...
Use this instead:
iMap[i, j] = int.Parse(line[j].ToString());
You might also want to use int.TryParse
if you want to be able to handle invalid input.
You are reading characters from the input and storing their ASCII values. If you know they will always be digits, just subtract 48 from each value when storing it.
精彩评论