11 Replies - 762 Views - Last Post: 05 March 2013 - 01:02 PM Rate Topic: -----

#1 bucky112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-March 13

Parsing data from a text file into a array

Posted 04 March 2013 - 04:50 PM

Hi all, please can someone point me in the right direction since I'm having trouble with my current task. I'm making a programme that will read certain lines from a text file and should be able to store and display the required data under headings in a table( ideally). The text file looks like this:

[Summary-123]
2066	0	2066	0	0	0
195	0	0	48
2066	0	2066	0	0	0
195	0	0	48
0	0	0	0	0	0
195	0	0	48
0	2066

[Summary-TH]
2066	0	2066	0	0	0
195	0	0	48
0	2066

[HRZones]
195
176
156
137
117
98
0
0
0
0
0

[SwapTimes]

[Trip]
158
43
2067
-103
-87
298
484
7633

[HRData]
87	0	0	-115	0	50
87	0	0	-115	0	50
87	0	0	-115	0	50
87	0	0	-115	0	50
88	0	0	-115	0	50
84	0	0	-115	0	50
84	0	0	-114	0	50
83	0	0	-114	0	50
82	0	0	-114	0	50
82	0	0	-114	0	50
81	0	0	-114	0	50
82	0	0	-114	0	50
83	0	0	-114	0	50
83	0	0	-114	0	50
83	0	0	-114	0	50
83	0	0	-114	0	50
82	0	0	-114	0	50
81	0	0	-114	0	50
81	0	0	-114	0	50
80	0	0	-114	0	50
80	0	0	-114	0	50
79	0	0	-114	0	50
78	0	0	-115	0	50
79	64	0	-115	0	50
80	72	0	-115	0	50
82	117	0	-115	0	50
86	131	0	-115	0	50
88	159	0	-115	0	50
91	176	33	-115	173	55
95	194	33	-115	173	55
98	203	33	-115	173	55
101	216	33	-115	173	55
102	225	33	-115	173	55
105	234	33	-115	173	55
107	242	33	-115	173	55
109	248	33	-115	173	55
111	255	33	-115	173	55
111	260	0	-116	0	50
112	263	64	-116	203	3887
113	269	66	-116	200	4402
113	272	68	-116	199	4403
114	277	69	-116	201	3885
114	280	70	-116	202	3635
114	283	71	-116	208	3892
114	284	72	-116	216	4407
115	283	71	-116	229	4660
115	287	71	-116	230	5682
115	287	72	-117	219	5171
116	287	72	-117	223	5169
116	288	72	-117	219	4916
117	290	73	-117	230	4663
118	290	73	-117	244	4144
118	289	73	-117	240	4148
119	289	73	-117	241	3892
119	288	73	-117	235	4148
120	288	73	-117	235	4148
120	289	73	-117	221	3888
120	289	73	-117	224	4148
121	289	73	-117	231	3634
121	288	73	-117	239	3891
121	286	72	-117	246	4150
122	283	72	-117	246	4150
122	283	71	-117	244	3126
122	284	71	-117	236	3125
122	286	72	-117	241	3378
122	287	72	-117	243	3379
122	289	73	-117	231	3632
122	289	73	-117	234	3379
123	292	73	-118	235	3634
123	293	74	-118	234	3637
123	293	74	-118	230	3378
124	293	74	-118	243	4145
124	292	74	-118	244	3630
124	292	74	-118	236	3634
125	291	73	-118	235	3635
125	290	73	-118	235	3120
126	290	73	-118	231	2611
127	292	74	-118	236	2612
127	293	74	-118	238	3123
127	293	74	-118	225	2609
128	295	74	-118	234	2607
128	298	75	-118	234	2607
129	298	75	-118	222	2358
129	300	75	-118	233	2357
130	300	75	-118	233	2613



I'm only interested in the HR data and each column represents data from left to right "average_speed, top_speed,heart_rate, altitude, cadence, power" i need to find a way to read all the data after the "[HRData]" header and then put them in an array or list of some kind so i can then use them in a datagrid etc .

An output of something like
Top speed Heart rate  etc  etc
84        73          0   -124    0   50
84        73          0   -124    0   50
84        84          0   -124    0   50



If anyone could point me in the right direction and give some advice that would be very welcome. Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Parsing data from a text file into a array

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3178
  • View blog
  • Posts: 9,635
  • Joined: 05-May 12

Re: Parsing data from a text file into a array

Posted 04 March 2013 - 09:28 PM

So post your code of what you've done so far. If you don't have any code yet, how about an outline of how you plan on solving it?
Was This Post Helpful? 0
  • +
  • -

#3 bucky112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-March 13

Re: Parsing data from a text file into a array

Posted 05 March 2013 - 04:03 AM

Hi there I'm thinking the best way to approach it would be to use a StreamReader and use Readline to read each line. Add each result/line to a List<string> and continue through the file?
Was This Post Helpful? 0
  • +
  • -

#4 pharylon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 83
  • Joined: 01-September 12

Re: Parsing data from a text file into a array

Posted 05 March 2013 - 05:41 AM

Well, I'd think you would definitely want to use StreamReader, but instead of putting it in a List<String> I'd probably create an HRData() class that contained fields for each of the columns (Top Speed, Heart Rate, etc).

Once you've done that, you can use StreamReader to iterate through the file and create a List<HRData>.

This post has been edited by pharylon: 05 March 2013 - 05:46 AM

Was This Post Helpful? 0
  • +
  • -

#5 bucky112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-March 13

Re: Parsing data from a text file into a array

Posted 05 March 2013 - 05:53 AM

I've managed to read in the text file and split it into the correct columns but in its simplest form I need it to skip/ignore the text until it reads '[HRData]' This is proving harder than I thought, if you could have a look at the code and get back tome that would be great.

namespace power_data
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            OpenFileDialog openfile = new OpenFileDialog();
            if (openfile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                using (var reader = new StreamReader(openfile.FileName))
                {
                    while (!reader.EndOfStream)
                    {
                        var line = reader.ReadLine();
                        var columns = line.Split('\t');
                        dataGridView1.Rows.Add(columns);
                    }
                }
            }

        }
    }
}




Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3178
  • View blog
  • Posts: 9,635
  • Joined: 05-May 12

Re: Parsing data from a text file into a array

Posted 05 March 2013 - 05:57 AM

So you are reading the line on line 20, and splitting into columns on line 21. Why don't you set a flag to true if you've seen the target section header. If you've not seen it yet, just continue on the while loop. If you've seen it, then split the line.
Was This Post Helpful? 0
  • +
  • -

#7 bucky112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-March 13

Re: Parsing data from a text file into a array

Posted 05 March 2013 - 06:07 AM

Hi there that seems like a better technique than what I was thinking,is there any chance you could give me a simple example of how i could use a flag within my code. i'm new to c# and have been struggling with this part for quite a while now.Thanks
Was This Post Helpful? 0
  • +
  • -

#8 pharylon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 83
  • Joined: 01-September 12

Re: Parsing data from a text file into a array

Posted 05 March 2013 - 06:32 AM

This is a great time to use a boolean flag!

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openfile = new OpenFileDialog();
            if (openfile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                using (var reader = new StreamReader(openfile.FileName))
                {
                    while (!reader.EndOfStream)
                    {
                        bool HRData = false;
                        var line = reader.ReadLine();
                        if (HRData)
                        {
                            var columns = line.Split('\t');
                            dataGridView1.Rows.Add(columns);
                        }
                        if (line.Contains("[HRData]"))
                            HRData = true;
                    }
                }
            }
        }


This post has been edited by pharylon: 05 March 2013 - 06:37 AM

Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3178
  • View blog
  • Posts: 9,635
  • Joined: 05-May 12

Re: Parsing data from a text file into a array

Posted 05 March 2013 - 07:10 AM

pharylon has the general idea of what you need. I think that the declaration and initialization of the flag is at the wrong scope level, though.
Was This Post Helpful? 0
  • +
  • -

#10 Momerath  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 996
  • View blog
  • Posts: 2,403
  • Joined: 04-October 09

Re: Parsing data from a text file into a array

Posted 05 March 2013 - 09:56 AM

And your thinking is right. Since it's reset to false at the top of the while loop, it will never find the data.
Was This Post Helpful? 0
  • +
  • -

#11 bucky112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-March 13

Re: Parsing data from a text file into a array

Posted 05 March 2013 - 10:59 AM

Thanks for all your help, the code pharylon didnt seem to work so i made some adjustments and got it.
private void button1_Click(object sender, EventArgs e)
        {
            string hrm;
            OpenFileDialog openfile = new OpenFileDialog();
            if (openfile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                using (var reader = new StreamReader(openfile.FileName))
                {
                    // reader.ReadLine();
                    //while (!reader.EndOfStream)
                    //{
                    //    var line = reader.ReadLine();
                    //    var columns = line.Split('\t');
                    //    dataGridView1.Rows.Add(columns);
                    //}

                    // set Flag
                    bool read = false;
                    string line = "";

                    while ((hrm = reader.ReadLine()) != null)
                    {
                        if (hrm.IndexOf("[HRData]") != -1)
                        {
                            read = true;
                        }

                        if (read)
                        {
                            line = reader.ReadLine();
                            var columns = line.Split('\t');
                            dataGridView1.Rows.Add(columns);
                        }
                    }
                }
            }


Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3178
  • View blog
  • Posts: 9,635
  • Joined: 05-May 12

Re: Parsing data from a text file into a array

Posted 05 March 2013 - 01:02 PM

Good job!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1