Reading data from a .txt file using C#

Reading data from a .txt file using C#

ganderson
Advocate Advocate
2,057 Views
5 Replies
Message 1 of 6

Reading data from a .txt file using C#

ganderson
Advocate
Advocate

Hi Folks,

    I have a txt file that has some data that I would like to use in a C# program that I am writing. Is there an example or code snippet that I can look at that shows how to open a file and parse the data. An example of the data is listed below:

 

1  Arm Hole Circumference Left: 19.761
1  Arm Hole Circumference Right: 21.122
1  Bicep Circumference Left: 12.851
1  Bicep Circumference Right: 13.365
1  Forearm Circumference Left: 10.44
1  Forearm Circumference Right: 10.189
1  Wrist Circumference Left: 7.1036
1  Wrist Circumference Right: 6.537
1  Arm Length Left: 22.749
1  Arm Length Right: 24.094
1  Arm Under Length Left: 16.361
1  Arm Under Length Right: 17.703

 

In all cases, the number at the end of the line is the one that I am interested in brining into the C# program.

 

Thanks in advance for your help.

                              ~Vonnie

0 Likes
2,058 Views
5 Replies
Replies (5)
Message 2 of 6

_gile
Consultant
Consultant

Hi,

 

Maybe something like this (not tested)

 

 

var result = new List<double>();
using (var reader = new StreamReader(filename))
{
    string line;
while ((line = reader.ReadLine()) != null) { var words = string.Split(' '); result.Add(double.Parse(words[words.Length - 1])); } } return result;

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 6

_Tharwat
Advisor
Advisor

Maybe something like this would give you a push Smiley Happy

 

        [CommandMethod("ReadTxt", CommandFlags.Modal)]
        public static void ReadTxtFileToCommandLine()
        {
            Document AcDoc = App.DocumentManager.MdiActiveDocument;
            Editor msg = AcDoc.Editor;
            try
            {
                OpenFileDialog ofd = new OpenFileDialog();
                if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string FileName = ofd.FileName;
                    FileStream Fstream = new FileStream(FileName, FileMode.Open);
                    StreamReader read = new StreamReader(Fstream);
                    string st = read.ReadLine();
                    while (st != null)
                    {
                        msg.WriteMessage("\n" + (st.Substring(st.IndexOf(':') + 2)));
                        st = read.ReadLine();
                    }
                    Fstream.Dispose();
                }
            }
            catch (System.Exception err)
            {
                msg.WriteMessage(err.Message);
            }
        }
0 Likes
Message 4 of 6

ganderson
Advocate
Advocate
Thanks, I'll try this..
0 Likes
Message 5 of 6

ganderson
Advocate
Advocate
Thanks!
0 Likes
Message 6 of 6

BKSpurgeon
Collaborator
Collaborator

as for parsing the text file - a simple google search will yeild plenty of results because it has been done a billion times below. I just did one and came up with a good number of results and code too - freely available. 

 

As for getting that last number - perhaps you will need to make use of the Regex class.

 

if you have trouble with you code please feel free to post again, but i think if you go in the general direction i've outlined above, you can't lose.

0 Likes