Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Reading Text file

Reading Text file

Scheduled Pinned Locked Moved C#
csharpdata-structuresquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LiamD
    wrote on last edited by
    #1

    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

    M A 2 Replies Last reply
    0
    • L LiamD

      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

      M Offline
      M Offline
      mav northwind
      wrote on last edited by
      #2

      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

      L 1 Reply Last reply
      0
      • M mav northwind

        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

        L Offline
        L Offline
        LiamD
        wrote on last edited by
        #3

        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

        M 1 Reply Last reply
        0
        • L LiamD

          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

          M Offline
          M Offline
          mav northwind
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • L LiamD

            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

            A Offline
            A Offline
            Anthony Baraff
            wrote on last edited by
            #5

            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;

            L 1 Reply Last reply
            0
            • A Anthony Baraff

              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;

              L Offline
              L Offline
              LiamD
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups