Solved

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

Posted on 2004-10-12
3
3,256 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
Comment Utility
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
Comment Utility
Thanks for the help..!!
0
 
LVL 8

Expert Comment

by:Razzie_
Comment Utility
No problem, glad I could help.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now