2 Replies - 4021 Views - Last Post: 24 March 2011 - 07:44 AM Rate Topic: -----

#1 cwd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-March 11

Read certain lines from text file

Posted 24 March 2011 - 06:06 AM

Hi again, I have this code now doing what I want pretty much (Thanks to you guys helping me out this far), and that is creating my chart from data off a text file. The final task I need to be able to do, to complete my program is have a way to select how many days of data to populate my chats with via a Text Box input.

I am thinking I will need to read the complete text file first to count how many lines are in it, then what ever is put in the text box it will take that many lines of data from the text file and populate the chart. For this to work correctly it would have to read the lines of the file back wards.(last line of file would be the first line..) e.g. textbox1.text = "7" then the chart would show only the last 7 lines of my data.

I was looking up examples of Counting the lines in the file, I think I am close here...
Dim fso As New FileSystemObject
Dim txt As TextStream
    
Set txt = fso.OpenTextFile(ReadChart, ForReading)
txt.ReadAll
    
Textbox2.text & txt.Line & " line(s)."
    
txt.Close
    
Set fso = Nothing




I need someone to guide me what I need to read up on to get this done step by step. I also found this http://www.dreaminco...from-text-file/ and it looks like what I need but just for the first part.



I think I need to place something in this part of the code to tell StreamReader how many lines to read...?
While ChartValues.Peek <> -1

            Dim values() As String = ChartValues.ReadLine.Split(","c)



            s.Points.AddXY(values(0), values(1))

            t.Points.AddXY(values(0), values(2))

            u.Points.AddXY(values(0), values(3))

        End While





This is my current code below
 Dim ReadChart As String
        ReadChart = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\Charts\chart.txt"

        Dim ChartValues As StreamReader = New StreamReader(ReadChart)

        Chart1.Series.Clear()

        Chart1.Titles.Add("Progress Chart")

        'Create a new series and add data points to it.  
        Dim s As New Series
        Dim t As New Series
        Dim u As New Series

        ' Name of the series
        s.Name = "Weight"
        t.Name = "Oxygen"
        u.Name = "Heartrate"

        'Change to a line graph.  
        s.ChartType = SeriesChartType.Spline
        t.ChartType = SeriesChartType.Spline
        u.ChartType = SeriesChartType.Spline

        While ChartValues.Peek <> -1

            Dim values() As String = ChartValues.ReadLine.Split(","c)



            s.Points.AddXY(values(0), values(1))

            t.Points.AddXY(values(0), values(2))

            u.Points.AddXY(values(0), values(3))

        End While


        'Add the series to the Chart1 control.  
        Chart1.Series.Add(s)
        Chart1.Series.Add(t)
        Chart1.Series.Add(u)


Thanks again for reading this, I started this new thread I hope it is ok.

This post has been edited by cwd: 24 March 2011 - 06:08 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Read certain lines from text file

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1606
  • View blog
  • Posts: 5,163
  • Joined: 25-September 09

Re: Read certain lines from text file

Posted 24 March 2011 - 06:20 AM

Have you thought about storing this information in a database where you could then pull date ranges and various points of data with ease using sql?

Not saying it can't be done the way you're doing it, just offering another storage method.

Another way might be to read the entire file into a List(Of String) where you would be able get a count and more than likely pull lines from it using LINQ
Was This Post Helpful? 0
  • +
  • -

#3 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1606
  • View blog
  • Posts: 5,163
  • Joined: 25-September 09

Re: Read certain lines from text file

Posted 24 March 2011 - 07:44 AM

You can get the total lines very simply with the ReadLines method of the File class:
Dim readText as IEnumerable(Of String) = File.ReadLines(ReadChart)
MessageBox.Show(readText.Count) 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1