How do i Read a set of values from a .txt file into a usable array?

Locked How do i Read a set of values from a .txt file into a usable array?

  • domingo, 22 de abril de 2012 19:29
     
     

    Here is an example of the data i need to read from the .txt:

    -6.28319, 1
    -6.15752, 0.992115
    -6.03186, 0.968583
    -5.90619, 0.929776
    -5.78053, 0.876307
    -5.65487, 0.809017
    -5.5292, 0.728969
    -5.40354, 0.637424

    Currently I have a load function from a menu strip that is working to load the file into a string.

    private string s = " ";

            private void openToolStripMenuItem_Click(object sender, EventArgs e)

            {

                string stemp;

                //Loading the File for Reading.

                FileStream fin;

                OpenFileDialog fileChooser = new OpenFileDialog();

                fileChooser.Filter = "Text files (*.txt)|*.txt|All files|*.*";

                //.Filter is what allows you to find a type of file.//

                DialogResult result = fileChooser.ShowDialog();

                //fileChooser is a class that lets you pick or not pick something.

                //ShowDialog is a method that pops up the GUI that lets you select the file.

                if (result == DialogResult.Cancel)

                    return;

                fin = new FileStream(fileChooser.FileName, FileMode.Open, FileAccess.Read);

                StreamReader fstrIn = new StreamReader(fin);

                while ((stemp = fstrIn.ReadLine()) != null)

                    s += stemp;

                fin.Close();

                fstrIn.Close();

    }

    From here i get lost as to what i need to do to convert the string to a 2D array.

    The Goal of this project is to take this array and use it to paint a series of lines connecting these points as (x,y) coordinates.  I have the paint function and the GUI all worked out, but This step has me lost.

    Thanks for any help.

Todas as Respostas

  • domingo, 22 de abril de 2012 23:03
     
     

    You could use ReadAllLines to read the file into an array of strings instead of a single string.

    Then loop through the array splitting each string on the comma to get the two parts. Then create a point (using PointF?) from the two parts (using Double.Parse or TryParse).


    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

  • segunda-feira, 23 de abril de 2012 10:00
     
     Respondido Contém Código

    Hi Forest9914,
    You can use a List to store all the PointF(If you use it). Please check following code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Drawing;
    
    namespace ConsoleApplicationTemp5
    {
        class Program
        {
            static void Main(string[] args)
            {
                String path = @"C:\Users\alexandersun\Desktop\2.txt";
                String rString = null;
                String[] tdArray = null;
                List<PointF> pList = new List<PointF>();
                using (FileStream fs = new FileStream(path, FileMode.Open))
                {
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        while ((rString = sr.ReadLine()) != null)
                        {
                            tdArray = new String[2];
                            tdArray = rString.Split(new Char[] { ',' });
                            PointF p = new PointF();
                            Single temp;
                            Single.TryParse(tdArray[0], out temp);
                            p.X = temp;
                            Single.TryParse(tdArray[1], out temp);
                            p.Y = temp;
                            pList.Add(p);
                        }
                    }
                }
            }
        }
    }

    I hope it helps.

    Have a nice day.


    Alexander Sun

  • terça-feira, 8 de maio de 2012 04:19
     
     
    I eventually got this working with some advice from my professor.  I cannot remember off the top of my head what the final technique was other than a bit sloppy.  Thanks for the help though!  I was not sure what the PointF was.
  • terça-feira, 8 de maio de 2012 09:11
    Moderador
     
     

    Hi Forest9914,

    PointF structure represents an ordered pair of floating-point x- and y-coordinates that defines a point in a two-dimensional plane. You can refer to this page: http://msdn.microsoft.com/en-us/library/system.drawing.pointf(v=vs.100).aspx

    Have a nice day.

    Best Regards,


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us