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?
Printable View
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?
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?
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.
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?
Well why not just store it all in an array and get the last 10 from the array?
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.
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).
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?
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....)
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.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