Reading Text file
-
I am still trying to get to grips with all the possibilities in C#. I am look for suggestion for the best way to implement the following. I have a text file in Comma Separated Value (CSV) with 7 columns of double values. On a load of the form I want to be able to read these values to be displayed some time later. My thinking is to store the data values in a 2 dimensional array. I have to know the number of fields to declare the array. I do not know the number of lines in the file - so I can't create the 2-dim array up front. So do I read the file to determine the number of lines then create the array? Then re-read to assign the values to the array. By the way I am using TextReader tr = new StreamReader(Filename); Alternatively, I did see the ArrayList type. I could just add to this list without having to define ot up front. I did not like this as it did not seem to be typed - I want to use doubles. What would be the best way to go? Should I use some other C# method? Ta, Liam
-
I am still trying to get to grips with all the possibilities in C#. I am look for suggestion for the best way to implement the following. I have a text file in Comma Separated Value (CSV) with 7 columns of double values. On a load of the form I want to be able to read these values to be displayed some time later. My thinking is to store the data values in a 2 dimensional array. I have to know the number of fields to declare the array. I do not know the number of lines in the file - so I can't create the 2-dim array up front. So do I read the file to determine the number of lines then create the array? Then re-read to assign the values to the array. By the way I am using TextReader tr = new StreamReader(Filename); Alternatively, I did see the ArrayList type. I could just add to this list without having to define ot up front. I did not like this as it did not seem to be typed - I want to use doubles. What would be the best way to go? Should I use some other C# method? Ta, Liam
If you want it strong-typed then you'll have to create your own collection. But first I'd define a class to hold the 7 values (each value does have a separate meaning, doesn't it?), so that each class instance represents a line in your file. Then I'd create a strong-typed collection of this class. You can inherit from CollectionBase and override the methods neccessary to ensure type-safety of your collection. Regards, mav
-
If you want it strong-typed then you'll have to create your own collection. But first I'd define a class to hold the 7 values (each value does have a separate meaning, doesn't it?), so that each class instance represents a line in your file. Then I'd create a strong-typed collection of this class. You can inherit from CollectionBase and override the methods neccessary to ensure type-safety of your collection. Regards, mav
I may have over 5000 lines in the file. Are you saying that I should have 5000 instances of this class? Yes each value has a separate meaning. I am not familiar with CollectionBase I will have a look into this. So do I perform something like this:-
TextReader tr = new StreamReader(Filename); while (( text_line = tr.ReadLine() ) != null ) { string[] arrStr = text_line.Split(','); for ( i=0; i
-
I may have over 5000 lines in the file. Are you saying that I should have 5000 instances of this class? Yes each value has a separate meaning. I am not familiar with CollectionBase I will have a look into this. So do I perform something like this:-
TextReader tr = new StreamReader(Filename); while (( text_line = tr.ReadLine() ) != null ) { string[] arrStr = text_line.Split(','); for ( i=0; i
If you want to have all the data in memory, then yes, you'd have 5000 instances of this class. You could, for example, add a static function to your class FromString() to perform parsing of a single input line and return a new instance holding the correct values. This instance would then be added to your collection. Something like this:
MyClassCollection coll = new MyClassCollection();
while((text_line = tr.ReadLine()) != null)
{
MyClass cls = MyClass.FromString(text_line);
coll.Add(cls);
}Regards, mav
-
I am still trying to get to grips with all the possibilities in C#. I am look for suggestion for the best way to implement the following. I have a text file in Comma Separated Value (CSV) with 7 columns of double values. On a load of the form I want to be able to read these values to be displayed some time later. My thinking is to store the data values in a 2 dimensional array. I have to know the number of fields to declare the array. I do not know the number of lines in the file - so I can't create the 2-dim array up front. So do I read the file to determine the number of lines then create the array? Then re-read to assign the values to the array. By the way I am using TextReader tr = new StreamReader(Filename); Alternatively, I did see the ArrayList type. I could just add to this list without having to define ot up front. I did not like this as it did not seem to be typed - I want to use doubles. What would be the best way to go? Should I use some other C# method? Ta, Liam
Something like this:
FileStream fs = new FileStream(_sFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
StreamReader reader = new StreamReader(fs);
ArrayList dates = new ArrayList();
ArrayList data = new ArrayList();
char[] sep = {','};
while(reader.Peek() != -1)
{
string[] rec = reader.ReadLine().Split(sep);
double[] record = new double[rec.Length];
for(int i = 0; i < rec.Length; i++)
{
if(Char.IsLetter(rec[i],0))
record[i] = 0d;
else
{
try
{
record[i] = double.Parse(rec[i]);
}
catch
{
record[i] = 0d;
}
}
}
data.Add(record);
}
double[][] dData = (double[][])data.ToArray(typeof(double[]));
reader.Close();
fs.Close();
reader = null;
fs = null; -
Something like this:
FileStream fs = new FileStream(_sFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
StreamReader reader = new StreamReader(fs);
ArrayList dates = new ArrayList();
ArrayList data = new ArrayList();
char[] sep = {','};
while(reader.Peek() != -1)
{
string[] rec = reader.ReadLine().Split(sep);
double[] record = new double[rec.Length];
for(int i = 0; i < rec.Length; i++)
{
if(Char.IsLetter(rec[i],0))
record[i] = 0d;
else
{
try
{
record[i] = double.Parse(rec[i]);
}
catch
{
record[i] = 0d;
}
}
}
data.Add(record);
}
double[][] dData = (double[][])data.ToArray(typeof(double[]));
reader.Close();
fs.Close();
reader = null;
fs = null;Anthony, EXCELLENT! 10/10. When it is presented as code it make things so much clearer. It is good to see how other people use the c# language. Your short piece of code has given me extra ideas how to use C#. Thanks you for your effort it is appreciated. Thanks, Liam