Question

I have a code to read text file and save the data into a double array to plot graph:

string filename = openFileDialog1.FileName;
var lineCount = 0;
using (var reader = File.OpenText(@filename))
{
     string line;
     while ((line = reader.ReadLine()) != null)
     {
          var data = line.Split(',');
          GlobalDataClass.dDataArray[lineCount, 0] = double.Parse(data[0]);
          GlobalDataClass.dDataArray[lineCount, 1] = double.Parse(data[1]);

          lineCount++;
     }
     ShowGraphData(lineCount);
}

And I have created a public class that initiate the array to [2000,2]:

static class GlobalDataClass
{
    public static double[,] dDataArray = new double[2000, 2];   
    public static long iTotalReadingPoint;

}    

My text file will be like:

0,29
1,31  
2,32  
3,32
4,30  

However I want my program to detect the EOF so that the text file can contain random rows and still able to plot the graph not restricted to 2000 rows. Is it possible?please advice.TQ

Was it helpful?

Solution

If you want to use same class then try given code. This is not most optimum memory usage wise but still this should work.

        string filename = openFileDialog1.FileName;
        var lineCount = 0;
        while ((line = reader.ReadLine()) != null)
        {
            if (GlobalDataClass.dDataArray.GetLength(0) == lineCount)
            {                
                double[,] newTemp = GlobalDataClass.dDataArray;
                int increaseLenght = newTemp.Length + 1000;
                GlobalDataClass.dDataArray = new double[increaseLenght, 2];
                Array.Copy(newTemp, GlobalDataClass.dDataArray, newTemp.LongLength);
            }

            var data = line.Split(',');
            GlobalDataClass.dDataArray[lineCount, 0] = double.Parse(data[0]);
            GlobalDataClass.dDataArray[lineCount, 1] = double.Parse(data[1]);
            lineCount++;
        }

OTHER TIPS

The StreamReader class has a boolean property named EndOfStream that is essentially EoF for FileStream objects, and there's the Peek() method which is usually the standard way of reading until EoF.

var reader = File.OpenText( ... );

while( reader.Peek() != -1 ) //Peek() returns -1 on EoF or if the stream is not seekable.
{
    var line = reader.ReadLine();
    ...
}

However reading from the file isn't really the problem if you don't want to be restricted to 2,000 lines. You might consider using a generic container of some flavour. For example:

using System.Collections.Generic;

string line = "12,34";
var dDataList = new List<KeyValuePair<double, double>>();
string[] parts = line.Split( ',' );
dDataList.Add( new KeyValuePair<double, double>( Double.Parse( parts[0] ), Double.Parse( parts[1] ) ) );

...

foreach( var pair in dDataList )
    Console.WriteLine( "{0} => {1}", pair.Key, pair.Value ); // 12 => 34

Will let you add as many pairs of doubles as you want, within reason, without having to fiddle with array resizing/copying or any of that nasty business. It's generally considered good practice to use a container like List<T> when dealing with an unknown amount of data, and to use arrays when you know exactly how much data you're going to have or the absolute maximum amount of data you can have.

I think you're asking your question a bit wrong; The problem you have is that you are declaring an array of 2000 unit fixed length, but you actually want it to be dynamic length. As Abhinav said, a list may work for you:

firstly, you might consider creating a class/struct for your coordinates:

public class Coordinate
{
    public double x;
    public double y;    
}  

Then, create a list of coordinates (of 0 length initially)...

static class GlobalDataClass
{
    public static List<Coordinate> dDataArray = new List<Coordinate>();
    public static long iTotalReadingPoint;
}    

And then add Coordinate objects to your list as you find new lines..

string filename = openFileDialog1.FileName;
var lineCount = 0;
using (var reader = File.OpenText(@filename))
{
     string line;
     while ((line = reader.ReadLine()) != null)
     {
          var data = line.Split(',');
          Coordinate coord = new Coordinate();
          coord.x = double.Parse(data[0]);
          coord.y = double.Parse(data[1]);
          GlobalDataClass.dDataArray.Add(coord);
          lineCount++;
     }
     ShowGraphData(lineCount);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top