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

Posted on 2004-10-12
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.
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?
Question by:gxp071
  • 2

Accepted Solution

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;

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];



Author Comment

ID: 12289224
Thanks for the help..!!

Expert Comment

ID: 12289855
No problem, glad I could help.

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Greetings C# program 17 104
Reset login password 4 42
Question! 4 36
SqlDependency to get update from sql to my c# app 2 37
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Internet Business Fax to Email Made Easy - With eFax Corporate (, 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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 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

16 Experts available now in Live!

Get 1:1 Help Now