Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read the last line only - in a Text File? 2

Status
Not open for further replies.

Acidon

Programmer
May 1, 2004
21
Hello,

I'm a real newbie, especially to C#, so forgive me..

What I would like to do is read the last line of a given text file into a string for manipulation. Could I have a suggestion on how to accomplish this? I only need to read it, I do not need to write the manipulated text back to the file.

Some of these text files that I would like to do this to can be quite large.. With only reading the last line, will it still take up a lot of resources because it's a large file?

Any help on this problem is greatly appreciated!

Thank you,
Acidon
 
well

String bla = new String;
while !(EOF)
{
//bla = readline method;
}

// well it exits the loop the string variable where you
// store the result of readline; will have the last line

// There must be a better and faster way ...
 
VBakias: I appreciate the response. I don't know why I didn't think of that, but regardless, thank you.

I will use that method until I figure out a more efficient way to do it. I am hoping that my app doesn't have to go through the entire file just to grab the last line. Hmm.. Is there a way to read a text file from the bottum up?

Your response not only got me moving again, but gave me a new way to look at the problem. I've just been typing while thinking.. I tend to do that.

Acidon
 
This is vb code

imports x = system.io

Dim m As New x.StreamReader("c:/test.txt")
Dim t As String = m.ReadToEnd
m.Close()
t = StrReverse(t)



in c# i think

using x = system.io;

StreamReader m = new StreamReader("c:/test.txt");
String t = new String(m.ReadToEnd);
m.Close();
// ***

///
with "t = StrReverse(t)" I replace the contents of the string t with the reversed string. So lastline becomes first. I don't know how this could be in C#
///
 
finally got it! (worked in VB)

imports x=system.io

Dim m As New x.StreamReader("c:/test.txt")
Dim t As String = m.ReadToEnd
m.Close()
MsgBox(t.Substring(t.LastIndexOf(System.Environment.NewLine)))



There are some syntax errors, but you can clear them out; Right?

using x = System.IO;

x.StreamReader m = new StreamReader("c:/test.txt");
String t = new String(m.ReadToEnd);
m.Close();

t.Substring(t.LastIndexOf(System.Environment.NewLine));

// the last big line of code (it is a string) has the last line of the text file located at "c:/test.txt".

 
VBakias, a couple of points:

Firstly -
You don't need:
Imports x = System.IO
or using x = System.IO;

you can simply use:
Imports System.IO
using System.IO;

which would give you:
Dim m As New StreamReader("c:/test.txt")
or StreamReader m = new StreamReader("c:/test.txt");

Secondly -
What happens if the file ends with CR/LF ie System.Environment.NewLine?

That is sometihing that may need to be tested for, additionally your code includes the leading CR/LF.

The following is a VB.NET example that solves both of those situations - and shouldn't be too difficult to convert to C#. To save reading a file I've just assigned a couple of variables.

Code:
    Dim nl As String = System.Environment.NewLine
    Dim nllen As Integer = nl.Length
    Dim s1 As String = "This is " + nl + "a multiline " + nl + "and very long sentence" + nl
    Dim s2 As String
    If s1.LastIndexOf(nl) = s1.Length - nllen Then
      s2 = s1.Substring(0, s1.Length - nllen).Substring(s1.Substring(0, s1.Length - nllen).LastIndexOf(nl) + nllen)
    Else
      s2 = s1.Substring(s1.LastIndexOf(nl) + 2)
    End If
    MessageBox.Show("(1)" + s1 + nl + "(2)" + s2 + "(3)END MARKER")

Try it both with and without + nl at the end of the s1 assignment.

It may be that having a leading CR/LF is not of any importance to the exercise, however unless a test is done for a trailing CR/LF any empty string may be returned.

Hope this helps.
 
Thank you both very much!

earthandfire, that was exactly what I needed. I was able convert that to C# without any problems and it works perfectly in all of my testing. Thank you!

I always get answers both quick and correct here. I can usually find what I need by searching, but on occasion I need to post.

Thanks again guys,
Acidon
 
Thanks to both of you!

earthandfire, that was exactly what I was looking for. I was able to convert it to C# and cater it to my app quickly and flawlessly.

Just wanted to say thanks to all that post trying to help others. I always get what I need here, whether I find it by searching or by posting the question.

And FYI, in case it matters, I'm not a student looking for someone else to solve my homework. I've been programming as a hobbyist off and on for about 20 years - yet unfortunately i've never stayed with it long enough to get real fluent in any one language (read: i'm not an advanced programmer). I am currently in love with C#, so I am learning all I can. I hope to someday write freeware and possibly some shareware to release to the world.

I'm rambling.. Thanks again all,
Acidon
 
Sorry for replying yet again ... I just wanted to apologize for the double post. The first time I posted I received an error. When I used the "back" button, my entire message was gone, so I typed it again and sent it through.

So, anyway .. Thanks
Acidon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top