-
December 8th, 2011, 01:20 AM
#1
Reading text file into 2D array
Hello guys,
I started learning C# after doing C++ for a year or so. At the moment I'm hung up on reading in a text file into a 2D array. I can't find the correct syntax anywhere. This is what I have.
[CODE]
public static void Main()
{
int[,] map;
FileStream fileStream = new FileStream(@"Map.txt", FileMode.Open);
map = new int[5,5];
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
map[i, j] = // What goes here?
}
}
}
[\CODE]
I've tried a few random things but can't get it right. Can someone let me know what I need? The array is a 5 by 5 grid of integers 1-15.
Thanks so much!
-
December 8th, 2011, 02:02 AM
#2
Re: Reading text file into 2D array
I have not used FileStream but you can accomplish the same with StreamReader. I am not sure if there is a performance difference between them but I'm sure the more experienced people will know.
Code:
int[,] map = new int[5, 5];
// Create reader
StreamReader reader = new StreamReader(@"Map.txt");
// Set up loops
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
// Try to convert to int but if it fails it will give it a value of 0 instead of crashing
Int32.TryParse(reader.ReadLine(), out map[i, j]);
}
}
// Close the reader
reader.Close();
-
December 8th, 2011, 08:02 PM
#3
Re: Reading text file into 2D array
Yeah, it wound up all being zeroes. How do I go about reading ints then?
-
December 9th, 2011, 12:27 AM
#4
Re: Reading text file into 2D array
Int32.TryParse()
Tries to convert the line into an int. Post what your Map.txt file so we can help you out better.
The code I provided you was assuming your file looked like this:
Code:
142
23
123
44
23
44
....
If its something like this instead:
Code:
24, 23, 44, 29, 123, 233 ...
Then you would have to split the line and convert each string into an int.
-
December 9th, 2011, 12:42 AM
#5
Re: Reading text file into 2D array
Originally Posted by Ubiquitous
Code:
// Try to convert to int but if it fails it will give it a value of 0 instead of crashing
Int32.TryParse(reader.ReadLine(), out map[i, j]);
If you use TryParse(...), you should (almost) always include error handling on the return value indicating parse success or failure. [Unless this is exactly the behavior you want - parsed if possible; zero otherwise]. Instead, use of Int32.Parse(...) is preferable exactly because it will crash on invalid input. It's better for a program to crash when assumptions about input are violated than it is to silently continue and give a wrong answer ("garbage in, garbage out").
Code:
if( !Int32.TryParse(reader.ReadLine(), out map[i,j]) )
{
//Handle errors with some code here
}
//Program continues...
But just a nitpick; nothing wrong with your solution in general. :-)
Best Regards,
BioPhysEngr
http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|