CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2011
    Posts
    2

    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!

  2. #2
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    70

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

  3. #3
    Join Date
    Dec 2011
    Posts
    2

    Re: Reading text file into 2D array

    Yeah, it wound up all being zeroes. How do I go about reading ints then?

  4. #4
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    70

    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.

  5. #5
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Reading text file into 2D array

    Quote Originally Posted by Ubiquitous View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured