Solved

Reading a 2d array from a text file using c#

Posted on 2004-10-12
3
3,302 Views
Last Modified: 2010-08-05
I have a two dimensional array contained within a text file, which has undefined dimensions in terms of rows and columns.
example
12.333 322.222 22.23
344.52 22.3 2334.2
333.22 3342.3 33.4
I wish to read in the matrix contained in the text file into a two-dimensional array in c#. How is this achieved and implemented?
0
Comment
Question by:gxp071
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
Razzie_ earned 500 total points
ID: 12288477
First read read through the entire file and count the number of lines. Also count the number of columns (seperated by a space). This code will work:

----------------------------------------------------------------------

String[,] array2D;

int numberOfLines = 0, numberOfColumns = 0;
string line;
System.IO.StreamReader sr = new System.IO.StreamReader(filename);
                  
while((line = sr.ReadLine()) != null)
{
      numberOfColumns = line.Split(' ').Length;
      numberOfLines++;
}
sr.Close();

array2D = new String[numberOfLines,numberOfColumns];
numberOfLines = 0;
                  
sr = new System.IO.StreamReader(filename);
while((line = sr.ReadLine()) != null)
{
      String[] tempArray = line.Split(' ');
      for(int i=0;i<tempArray.Length;++i)
      {
            array2D[numberOfLines, i] = tempArray[i];
      }
      numberOfLines++;
}
------------------------------------------------------------------------------------

HTH,

Razzie
0
 
LVL 6

Author Comment

by:gxp071
ID: 12289224
Thanks for the help..!!
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 12289855
No problem, glad I could help.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question