Sign in to follow this  
FantasyVII

Read 2D array map from text file

Recommended Posts

Hello everyone,

I have this text file
[CODE]
1234
5678
[/CODE]

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

[CODE]
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[i];
for (int j = 0; j < line.Length; j++)
{
map[i, j] = (int)(line[j] - '0');
Console.Write(map[i, j]);
}
}
Console.ReadKey();
}
}
[/CODE]

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.
[CODE]
1 2 3 4
5 6 7 8
[/CODE]

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.

[CODE]
1 2 3 4
5 6 7 8
9 10 11 12
[/CODE]

Share this post


Link to post
Share on other sites
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);
}

Share this post


Link to post
Share on other sites
[quote name='Neometron' timestamp='1343527847' post='4964179']
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);
}
[/quote]

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this