Jump to content

View more

View more

Image of the Day

Isn't this a lovely apple tempart placeholder thing  #gamedev worth a #screenshotsaturday even I would say. https://t.co/fQH1d0ySIG
IOTD | Top Screenshots

The latest, straight to your Inbox.

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


Sign up now

Read 2D array map from text file

2: Adsense

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.


  • You cannot reply to this topic
2 replies to this topic

#1 FantasyVII   Members   

1073
Like
0Likes
Like

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

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 


#2 Neometron   Members   

520
Like
1Likes
Like

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

#3 FantasyVII   Members   

1073
Like
0Likes
Like

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.