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 as an array by using streamreader

reading text file as an array by using streamreader

Scheduled Pinned Locked Moved C#
csharpdata-structures
8 Posts 7 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.
  • M Offline
    M Offline
    Member_14571520
    wrote on last edited by
    #1

    Text File in Notepad++

    1. Name
    2. Institute
    3. .......................................................................
    4. x : y
    5. ......................................................................
    6. 9 7
    7. .......................................................................
    8. Mass(M) : Weight(W) : Time(T) : Velocity(V) : Acceleration(A) : Distance(D)
    9. .......................................................................
    10. 4.0 5.0 3.56 4.78 5.098 2
    11. 5.0 6.0 4.67 5.87 6.387 3
    12. 3.0 4.0 6.49 3.95 5.209 4
    13. 4.0 7.0 2.01 6.98 2.039 9
    14. 9.0 2.0 5.02 2.94 7.209 2
    15. 8.0 2.0 4.02 7.30 8.202 6
    16. 5.0 9.0 7.03 3.06 2.049 3
    17. 0.5 0.4 0.34 0.34 0.534 9
    18. 0.7 0.2 0.53 0.74 0.293 3
    19. ........................................................................
    20.
    21.

    Code in C#

    using System;
    using System.IO;
    using System.Globalization;

    namespace main
    {
    public class project
    {
    public int x, y;
    public matrix MandW, Time, VandA, Distance;
    public string cmt, file, dash;
    public project()
    {
    string filename= "C://user//abc.txt";
    Read(filename);
    }
    public void Read(String pfile)
    {
    file=pfile;
    CultureInfo _ci = CultureInfo.InvariantCulture;
    using (StreamReader _sr = new StreamReader(pfile))
    {
    string[] values;
    cmt = _sr.ReadLine(); /* reading keyword Name */
    cmt = _sr.ReadLine(); /* reading keyword institute */
    dash = _sr.ReadLine(); /* reading ............*/
    cmt = _sr.ReadLine(); /* reading x : y */
    dash = _sr.ReadLine(); /*reading ..............*/
    values = IO.split_string(_sr.ReadLine());
    x = Convert.ToInt32(values[0]);
    y = Convert.ToInt32(values[1]);
    dash = _sr.ReadLine(); /*reading ...............*/
    cmt = _sr.ReadLine(); /*reading Mass(M) : Weight(W) :... */
    dash = _sr.ReadLine();
    MandW = new matrix(x,2); /* Mass and Weight Matrix*/
    for (int i= 0, i

    I am having trouble constructing these matrices..
    Thank you in advance.

    OriginalGriffO L M B G 7 Replies Last reply
    0
    • M Member_14571520

      Text File in Notepad++

      1. Name
      2. Institute
      3. .......................................................................
      4. x : y
      5. ......................................................................
      6. 9 7
      7. .......................................................................
      8. Mass(M) : Weight(W) : Time(T) : Velocity(V) : Acceleration(A) : Distance(D)
      9. .......................................................................
      10. 4.0 5.0 3.56 4.78 5.098 2
      11. 5.0 6.0 4.67 5.87 6.387 3
      12. 3.0 4.0 6.49 3.95 5.209 4
      13. 4.0 7.0 2.01 6.98 2.039 9
      14. 9.0 2.0 5.02 2.94 7.209 2
      15. 8.0 2.0 4.02 7.30 8.202 6
      16. 5.0 9.0 7.03 3.06 2.049 3
      17. 0.5 0.4 0.34 0.34 0.534 9
      18. 0.7 0.2 0.53 0.74 0.293 3
      19. ........................................................................
      20.
      21.

      Code in C#

      using System;
      using System.IO;
      using System.Globalization;

      namespace main
      {
      public class project
      {
      public int x, y;
      public matrix MandW, Time, VandA, Distance;
      public string cmt, file, dash;
      public project()
      {
      string filename= "C://user//abc.txt";
      Read(filename);
      }
      public void Read(String pfile)
      {
      file=pfile;
      CultureInfo _ci = CultureInfo.InvariantCulture;
      using (StreamReader _sr = new StreamReader(pfile))
      {
      string[] values;
      cmt = _sr.ReadLine(); /* reading keyword Name */
      cmt = _sr.ReadLine(); /* reading keyword institute */
      dash = _sr.ReadLine(); /* reading ............*/
      cmt = _sr.ReadLine(); /* reading x : y */
      dash = _sr.ReadLine(); /*reading ..............*/
      values = IO.split_string(_sr.ReadLine());
      x = Convert.ToInt32(values[0]);
      y = Convert.ToInt32(values[1]);
      dash = _sr.ReadLine(); /*reading ...............*/
      cmt = _sr.ReadLine(); /*reading Mass(M) : Weight(W) :... */
      dash = _sr.ReadLine();
      MandW = new matrix(x,2); /* Mass and Weight Matrix*/
      for (int i= 0, i

      I am having trouble constructing these matrices..
      Thank you in advance.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Member 14571520 wrote:

      I am having trouble constructing these matrices..

      What trouble? What does it do that you didn't expect, or not do that you did? Any error messages? What have you tried to fix it?

      Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • M Member_14571520

        Text File in Notepad++

        1. Name
        2. Institute
        3. .......................................................................
        4. x : y
        5. ......................................................................
        6. 9 7
        7. .......................................................................
        8. Mass(M) : Weight(W) : Time(T) : Velocity(V) : Acceleration(A) : Distance(D)
        9. .......................................................................
        10. 4.0 5.0 3.56 4.78 5.098 2
        11. 5.0 6.0 4.67 5.87 6.387 3
        12. 3.0 4.0 6.49 3.95 5.209 4
        13. 4.0 7.0 2.01 6.98 2.039 9
        14. 9.0 2.0 5.02 2.94 7.209 2
        15. 8.0 2.0 4.02 7.30 8.202 6
        16. 5.0 9.0 7.03 3.06 2.049 3
        17. 0.5 0.4 0.34 0.34 0.534 9
        18. 0.7 0.2 0.53 0.74 0.293 3
        19. ........................................................................
        20.
        21.

        Code in C#

        using System;
        using System.IO;
        using System.Globalization;

        namespace main
        {
        public class project
        {
        public int x, y;
        public matrix MandW, Time, VandA, Distance;
        public string cmt, file, dash;
        public project()
        {
        string filename= "C://user//abc.txt";
        Read(filename);
        }
        public void Read(String pfile)
        {
        file=pfile;
        CultureInfo _ci = CultureInfo.InvariantCulture;
        using (StreamReader _sr = new StreamReader(pfile))
        {
        string[] values;
        cmt = _sr.ReadLine(); /* reading keyword Name */
        cmt = _sr.ReadLine(); /* reading keyword institute */
        dash = _sr.ReadLine(); /* reading ............*/
        cmt = _sr.ReadLine(); /* reading x : y */
        dash = _sr.ReadLine(); /*reading ..............*/
        values = IO.split_string(_sr.ReadLine());
        x = Convert.ToInt32(values[0]);
        y = Convert.ToInt32(values[1]);
        dash = _sr.ReadLine(); /*reading ...............*/
        cmt = _sr.ReadLine(); /*reading Mass(M) : Weight(W) :... */
        dash = _sr.ReadLine();
        MandW = new matrix(x,2); /* Mass and Weight Matrix*/
        for (int i= 0, i

        I am having trouble constructing these matrices..
        Thank you in advance.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Your main problem seems to be that you do no checks on the data you are reading. You should check the content of each line to ensure it contains the elements you expect. You should also check the result of your calls to IO.split_string; and why are you not using string.split()? And do not use Convert to convert strings to numbers, use the TryParse() method for each type; that way you will not be caught out by bad data. And finally, if you look at your data the lines containing the mass and weight values have a line number at the beginning which will confuse the converter: 10. 4.0 5.0 3.56 4.78 5.098 2.

        1 Reply Last reply
        0
        • M Member_14571520

          Text File in Notepad++

          1. Name
          2. Institute
          3. .......................................................................
          4. x : y
          5. ......................................................................
          6. 9 7
          7. .......................................................................
          8. Mass(M) : Weight(W) : Time(T) : Velocity(V) : Acceleration(A) : Distance(D)
          9. .......................................................................
          10. 4.0 5.0 3.56 4.78 5.098 2
          11. 5.0 6.0 4.67 5.87 6.387 3
          12. 3.0 4.0 6.49 3.95 5.209 4
          13. 4.0 7.0 2.01 6.98 2.039 9
          14. 9.0 2.0 5.02 2.94 7.209 2
          15. 8.0 2.0 4.02 7.30 8.202 6
          16. 5.0 9.0 7.03 3.06 2.049 3
          17. 0.5 0.4 0.34 0.34 0.534 9
          18. 0.7 0.2 0.53 0.74 0.293 3
          19. ........................................................................
          20.
          21.

          Code in C#

          using System;
          using System.IO;
          using System.Globalization;

          namespace main
          {
          public class project
          {
          public int x, y;
          public matrix MandW, Time, VandA, Distance;
          public string cmt, file, dash;
          public project()
          {
          string filename= "C://user//abc.txt";
          Read(filename);
          }
          public void Read(String pfile)
          {
          file=pfile;
          CultureInfo _ci = CultureInfo.InvariantCulture;
          using (StreamReader _sr = new StreamReader(pfile))
          {
          string[] values;
          cmt = _sr.ReadLine(); /* reading keyword Name */
          cmt = _sr.ReadLine(); /* reading keyword institute */
          dash = _sr.ReadLine(); /* reading ............*/
          cmt = _sr.ReadLine(); /* reading x : y */
          dash = _sr.ReadLine(); /*reading ..............*/
          values = IO.split_string(_sr.ReadLine());
          x = Convert.ToInt32(values[0]);
          y = Convert.ToInt32(values[1]);
          dash = _sr.ReadLine(); /*reading ...............*/
          cmt = _sr.ReadLine(); /*reading Mass(M) : Weight(W) :... */
          dash = _sr.ReadLine();
          MandW = new matrix(x,2); /* Mass and Weight Matrix*/
          for (int i= 0, i

          I am having trouble constructing these matrices..
          Thank you in advance.

          M Offline
          M Offline
          Maciej Los
          wrote on last edited by
          #4

          I haven't heard about IO.split_string() method and i can't find it in MSDN documentation... Can you share direct link into it?

          1 Reply Last reply
          0
          • M Member_14571520

            Text File in Notepad++

            1. Name
            2. Institute
            3. .......................................................................
            4. x : y
            5. ......................................................................
            6. 9 7
            7. .......................................................................
            8. Mass(M) : Weight(W) : Time(T) : Velocity(V) : Acceleration(A) : Distance(D)
            9. .......................................................................
            10. 4.0 5.0 3.56 4.78 5.098 2
            11. 5.0 6.0 4.67 5.87 6.387 3
            12. 3.0 4.0 6.49 3.95 5.209 4
            13. 4.0 7.0 2.01 6.98 2.039 9
            14. 9.0 2.0 5.02 2.94 7.209 2
            15. 8.0 2.0 4.02 7.30 8.202 6
            16. 5.0 9.0 7.03 3.06 2.049 3
            17. 0.5 0.4 0.34 0.34 0.534 9
            18. 0.7 0.2 0.53 0.74 0.293 3
            19. ........................................................................
            20.
            21.

            Code in C#

            using System;
            using System.IO;
            using System.Globalization;

            namespace main
            {
            public class project
            {
            public int x, y;
            public matrix MandW, Time, VandA, Distance;
            public string cmt, file, dash;
            public project()
            {
            string filename= "C://user//abc.txt";
            Read(filename);
            }
            public void Read(String pfile)
            {
            file=pfile;
            CultureInfo _ci = CultureInfo.InvariantCulture;
            using (StreamReader _sr = new StreamReader(pfile))
            {
            string[] values;
            cmt = _sr.ReadLine(); /* reading keyword Name */
            cmt = _sr.ReadLine(); /* reading keyword institute */
            dash = _sr.ReadLine(); /* reading ............*/
            cmt = _sr.ReadLine(); /* reading x : y */
            dash = _sr.ReadLine(); /*reading ..............*/
            values = IO.split_string(_sr.ReadLine());
            x = Convert.ToInt32(values[0]);
            y = Convert.ToInt32(values[1]);
            dash = _sr.ReadLine(); /*reading ...............*/
            cmt = _sr.ReadLine(); /*reading Mass(M) : Weight(W) :... */
            dash = _sr.ReadLine();
            MandW = new matrix(x,2); /* Mass and Weight Matrix*/
            for (int i= 0, i

            I am having trouble constructing these matrices..
            Thank you in advance.

            M Offline
            M Offline
            Maciej Los
            wrote on last edited by
            #5

            If the file content has always got the same structure, you can to grab data into specific class. For example:

            public class MyData
            {
            private System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
            public string Name {get; set;}
            public string Institute {get; set;}
            public int X {get; set;}
            public int Y {get; set;}
            public string[] Headers {get; set;}
            public double[][] DataMatrix {get; set;}
            public MyData(string sFileName)
            {
            if(!File.Exists(sFileName))
            throw new FileNotFoundException($"File '{Path.GetFileName(sFileName)}' does not exist!");

            	string\[\] lines = File.ReadAllLines(sFileName);
            	Name = lines\[0\];
            	Institute = lines\[1\];
            	X = Convert.ToInt32(lines\[5\].Split(' ')\[0\]);
            	Y = Convert.ToInt32(lines\[5\].Split(' ')\[1\]);
            	Headers = lines\[7\].Split(new string\[\]{" : "}, StringSplitOptions.RemoveEmptyEntries);
            	DataMatrix = lines.Skip(9)
            		.TakeWhile(l=>l.Length>0 && !l.StartsWith("."))
            		.Select(l=>l.Split(' ').Select(d=>Double.Parse(d, ci)).ToArray())
            		.ToArray();
            }
            

            }

            Then usage will be very easy:

            string filename= "C://user//abc.txt";
            MyData md = new MyData(filename);
            Console.WriteLine(md.Name);
            Console.WriteLine(md.Institute);
            Console.WriteLine("X = {0}, Y = {1}", md.X, md.Y);
            double[] allMasses = md.DataMatrix.Select(m=>m[0]).ToArray();
            foreach(double d in allMasses)
            Console.WriteLine(d);
            //on the same manner you can get Weight, Time, Velocity, Acceleration and Distance

            Good luck!

            1 Reply Last reply
            0
            • M Member_14571520

              Text File in Notepad++

              1. Name
              2. Institute
              3. .......................................................................
              4. x : y
              5. ......................................................................
              6. 9 7
              7. .......................................................................
              8. Mass(M) : Weight(W) : Time(T) : Velocity(V) : Acceleration(A) : Distance(D)
              9. .......................................................................
              10. 4.0 5.0 3.56 4.78 5.098 2
              11. 5.0 6.0 4.67 5.87 6.387 3
              12. 3.0 4.0 6.49 3.95 5.209 4
              13. 4.0 7.0 2.01 6.98 2.039 9
              14. 9.0 2.0 5.02 2.94 7.209 2
              15. 8.0 2.0 4.02 7.30 8.202 6
              16. 5.0 9.0 7.03 3.06 2.049 3
              17. 0.5 0.4 0.34 0.34 0.534 9
              18. 0.7 0.2 0.53 0.74 0.293 3
              19. ........................................................................
              20.
              21.

              Code in C#

              using System;
              using System.IO;
              using System.Globalization;

              namespace main
              {
              public class project
              {
              public int x, y;
              public matrix MandW, Time, VandA, Distance;
              public string cmt, file, dash;
              public project()
              {
              string filename= "C://user//abc.txt";
              Read(filename);
              }
              public void Read(String pfile)
              {
              file=pfile;
              CultureInfo _ci = CultureInfo.InvariantCulture;
              using (StreamReader _sr = new StreamReader(pfile))
              {
              string[] values;
              cmt = _sr.ReadLine(); /* reading keyword Name */
              cmt = _sr.ReadLine(); /* reading keyword institute */
              dash = _sr.ReadLine(); /* reading ............*/
              cmt = _sr.ReadLine(); /* reading x : y */
              dash = _sr.ReadLine(); /*reading ..............*/
              values = IO.split_string(_sr.ReadLine());
              x = Convert.ToInt32(values[0]);
              y = Convert.ToInt32(values[1]);
              dash = _sr.ReadLine(); /*reading ...............*/
              cmt = _sr.ReadLine(); /*reading Mass(M) : Weight(W) :... */
              dash = _sr.ReadLine();
              MandW = new matrix(x,2); /* Mass and Weight Matrix*/
              for (int i= 0, i

              I am having trouble constructing these matrices..
              Thank you in advance.

              B Offline
              B Offline
              BillWoodruff
              wrote on last edited by
              #6

              imho, the solution you really want requires you ... or someone else ... writing the code to serialize the data into XML where it can then be deserialized into an instance of a a Class, or added, by binding, to some grid UI. If that's not possible, I am afraid you are stuck with the tedious task of implementing a line-by-line parser with error-checking per line, as Richard suggested.

              «Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

              1 Reply Last reply
              0
              • M Member_14571520

                Text File in Notepad++

                1. Name
                2. Institute
                3. .......................................................................
                4. x : y
                5. ......................................................................
                6. 9 7
                7. .......................................................................
                8. Mass(M) : Weight(W) : Time(T) : Velocity(V) : Acceleration(A) : Distance(D)
                9. .......................................................................
                10. 4.0 5.0 3.56 4.78 5.098 2
                11. 5.0 6.0 4.67 5.87 6.387 3
                12. 3.0 4.0 6.49 3.95 5.209 4
                13. 4.0 7.0 2.01 6.98 2.039 9
                14. 9.0 2.0 5.02 2.94 7.209 2
                15. 8.0 2.0 4.02 7.30 8.202 6
                16. 5.0 9.0 7.03 3.06 2.049 3
                17. 0.5 0.4 0.34 0.34 0.534 9
                18. 0.7 0.2 0.53 0.74 0.293 3
                19. ........................................................................
                20.
                21.

                Code in C#

                using System;
                using System.IO;
                using System.Globalization;

                namespace main
                {
                public class project
                {
                public int x, y;
                public matrix MandW, Time, VandA, Distance;
                public string cmt, file, dash;
                public project()
                {
                string filename= "C://user//abc.txt";
                Read(filename);
                }
                public void Read(String pfile)
                {
                file=pfile;
                CultureInfo _ci = CultureInfo.InvariantCulture;
                using (StreamReader _sr = new StreamReader(pfile))
                {
                string[] values;
                cmt = _sr.ReadLine(); /* reading keyword Name */
                cmt = _sr.ReadLine(); /* reading keyword institute */
                dash = _sr.ReadLine(); /* reading ............*/
                cmt = _sr.ReadLine(); /* reading x : y */
                dash = _sr.ReadLine(); /*reading ..............*/
                values = IO.split_string(_sr.ReadLine());
                x = Convert.ToInt32(values[0]);
                y = Convert.ToInt32(values[1]);
                dash = _sr.ReadLine(); /*reading ...............*/
                cmt = _sr.ReadLine(); /*reading Mass(M) : Weight(W) :... */
                dash = _sr.ReadLine();
                MandW = new matrix(x,2); /* Mass and Weight Matrix*/
                for (int i= 0, i

                I am having trouble constructing these matrices..
                Thank you in advance.

                G Offline
                G Offline
                Gene M
                wrote on last edited by
                #7

                It may be common practice. Is your filename slightly slanted?

                1 Reply Last reply
                0
                • M Member_14571520

                  Text File in Notepad++

                  1. Name
                  2. Institute
                  3. .......................................................................
                  4. x : y
                  5. ......................................................................
                  6. 9 7
                  7. .......................................................................
                  8. Mass(M) : Weight(W) : Time(T) : Velocity(V) : Acceleration(A) : Distance(D)
                  9. .......................................................................
                  10. 4.0 5.0 3.56 4.78 5.098 2
                  11. 5.0 6.0 4.67 5.87 6.387 3
                  12. 3.0 4.0 6.49 3.95 5.209 4
                  13. 4.0 7.0 2.01 6.98 2.039 9
                  14. 9.0 2.0 5.02 2.94 7.209 2
                  15. 8.0 2.0 4.02 7.30 8.202 6
                  16. 5.0 9.0 7.03 3.06 2.049 3
                  17. 0.5 0.4 0.34 0.34 0.534 9
                  18. 0.7 0.2 0.53 0.74 0.293 3
                  19. ........................................................................
                  20.
                  21.

                  Code in C#

                  using System;
                  using System.IO;
                  using System.Globalization;

                  namespace main
                  {
                  public class project
                  {
                  public int x, y;
                  public matrix MandW, Time, VandA, Distance;
                  public string cmt, file, dash;
                  public project()
                  {
                  string filename= "C://user//abc.txt";
                  Read(filename);
                  }
                  public void Read(String pfile)
                  {
                  file=pfile;
                  CultureInfo _ci = CultureInfo.InvariantCulture;
                  using (StreamReader _sr = new StreamReader(pfile))
                  {
                  string[] values;
                  cmt = _sr.ReadLine(); /* reading keyword Name */
                  cmt = _sr.ReadLine(); /* reading keyword institute */
                  dash = _sr.ReadLine(); /* reading ............*/
                  cmt = _sr.ReadLine(); /* reading x : y */
                  dash = _sr.ReadLine(); /*reading ..............*/
                  values = IO.split_string(_sr.ReadLine());
                  x = Convert.ToInt32(values[0]);
                  y = Convert.ToInt32(values[1]);
                  dash = _sr.ReadLine(); /*reading ...............*/
                  cmt = _sr.ReadLine(); /*reading Mass(M) : Weight(W) :... */
                  dash = _sr.ReadLine();
                  MandW = new matrix(x,2); /* Mass and Weight Matrix*/
                  for (int i= 0, i

                  I am having trouble constructing these matrices..
                  Thank you in advance.

                  L Offline
                  L Offline
                  Lutoslaw
                  wrote on last edited by
                  #8

                  Member 14571520 wrote:

                  MandW = new matrix(x,2); /* Mass and Weight Matrix*/

                  Unless matrix is an actual class name, I think you wanted something like

                  MandW = new double[x,2]; /* Mass and Weight Matrix*/

                  Class names should by UpperCamelCase. Also I'm not sure if you should call Read() in a constructor... let external code decide when does it want to do this

                  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