• FEATURED

View more

View more

View more

\$50

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

## Read 2D array map from text file

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1FantasyVII  Members

Posted 28 July 2012 - 04:47 PM

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)
{

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]);
}
}
}
}


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


### #2Neometron  Members

Posted 28 July 2012 - 08:10 PM

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);
}

### #3FantasyVII  Members

Posted 29 July 2012 - 06:06 AM

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.