Results 1 to 10 of 10

Thread: [RESOLVED] Display Current Results In Text File

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    136

    Resolved [RESOLVED] Display Current Results In Text File

    How can I display only the most current results from a text file? I have a button the user can click that needs to display only the most current results from the text file. How can I do this?
    Last edited by twilitegxa; Apr 5th, 2010 at 07:53 PM.

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    136

    Re: Display Current Results In Text File

    I have to display all form results in a text file and I need to have a button that the user can press that can allow them to access the most recent post from the form. How can I access the most recent form post from my text file? Any help?

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Display Current Results In Text File

    You talk about "current results" as though we know what that means. A text file just contains text unless someone says different. You haven't explained anything about the structure of the data in your file so we can't tell you how to interpret that data. All we can really say is that you can either create a StreamReader and read the file line by line, or else you can use the File.ReadAllText or .ReadAllLines methods to read the entire file as a single String or an array of Strings. Beyond that, how to interpret the data you read depends on the format, and we know nothing about the format of your file.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    136

    Re: Display Current Results In Text File

    Is there a method I could write that would get the last record that was posted into the text file? Like if I used the StreamReader, could I just read the last say 10 lines?
    Last edited by twilitegxa; Apr 5th, 2010 at 09:43 PM.

  5. #5

    Re: Display Current Results In Text File

    Well why not just store it all in an array and get the last 10 from the array?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Display Current Results In Text File

    Quote Originally Posted by twilitegxa View Post
    Is there a method I could write that would get the last record that was posted into the text file? Like if I used the StreamReader, could I just read the last say 10 lines?
    Not really. If you knew the exact byte offset then you could position the pointer using the underlying FileStream, but that's not very likely. Generally speaking, you simply have to read and discard any text before the part you want. If you want the last 10 lines then you could call File.ReadAllLines and then just use the last 10 elements of the resultant String array. Alternatively, you could create a StreamReader and call ReadLine repeatedly and discard the result until you got to the part you wanted. For that second option, you'd have to know the number of lines to discard.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    136

    Re: Display Current Results In Text File

    How could I use the last 10 elements if I use the File.ReadAllLines option?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Display Current Results In Text File

    Quote Originally Posted by twilitegxa View Post
    How could I use the last 10 elements if I use the File.ReadAllLines option?
    ReadAllLines returns an array. It's used like any other array. If you want the last 10 elements then you could use the Length property to determine the indexes of those elements. You can then use a For loop or whatever's appropriate under the circumstances to access those elements. The last index of the last element, as always, can be determined using the GetUpperBound method. The 10th last element would be at index (Length - 10).
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    136

    Re: Display Current Results In Text File

    I have this so far:

    Private Sub btnCurrent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCurrent.Click
    Dim path As String = "C:\Documents and Settings\TwiLite\Desktop\Windows Programming - Laura Malave/MadEaters Solution\MadEaters\Survey.txt"

    Dim readText() As String = File.ReadAllLines(path)
    Dim s As String
    For Each s In readText
    lblResults.Text = s
    Next
    End Sub

    And this gets that last line of the text file, now can you explain a little more on how to get the last ten lines?

  10. #10

    Re: [RESOLVED] Display Current Results In Text File

    Ok, you can say that readText.GetUpperBound(0) would get the number of lines right?
    So, readText.GetUpperBound(0) - 10 will set you back enough lines (or is it - 9....)
    Code:
    Dim Start As Integer = readText.GetUpperBound(0) - 10
    Dim End As Integer = Start + 10
    Dim CurrentLine As String = String.Empty
    For i = Start To End
        CurrentLine = readText(i) '<-current line
    Next
    I didn't test that, but something like that would get you the last ten lines of your array. Just use conventional logic. You have the last line there because your For Loop goes through each line and the last line obviously is the one shown. So, you can use .GetUpperBound(0) to get the total number of lines, take away 10, and there's your gap. Just add one each time to get the appropriate line.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width