Read 2D array map from text file

Started by
1 comment, last by FantasyVII 11 years, 8 months ago
Hello everyone,

I have this text file

1234
5678


I can read this text file into a 2D int array and print it into the screen without any problem.


class Program
{
static int MapX = 26, MapY = 19;

static int[,] map;


static void Main(string[] args)
{
string[] buffer = File.ReadAllLines("C:/map.txt");

map = new int[buffer.Length, MapX];


string line;

for (int i = 0; i < buffer.Length; i++)
{
line = buffer;
for (int j = 0; j < line.Length; j++)
{
map[i, j] = (int)(line[j] - '0');
Console.Write(map[i, j]);
}
}
Console.ReadKey();
}
}


However this limits me to numbers from 0-9. So I can not store number 10 in the int array, because it will think its 1 and 0 not 10.

so I have been trying to read the same text file but with spaces between the numbers.

1 2 3 4
5 6 7 8


so I want to be able to read all numbers from 0 - 100 or what ever and then store these numbers into a 2D integer array which is called map.

can you guys please help me? How can I store these numbers into an array? keep in mind I also want to store numbers above 9.


1 2 3 4
5 6 7 8
9 10 11 12
Advertisement
I don't program in c# but would something like this work?

1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12

string[] astrNumbers = buffer.Split(',')

foreach( string strNumber in astrNumbers )
{
int numVal = Convert.ToInt32(strNumber);
}

I don't program in c# but would something like this work?

1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12

string[] astrNumbers = buffer.Split(',')

foreach( string strNumber in astrNumbers )
{
int numVal = Convert.ToInt32(strNumber);
}


thx, i'll try it out and see if it works

This topic is closed to new replies.

Advertisement