Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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


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,

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


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 ...

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

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.


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

This is vb code

imports x = system.io

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

in c# i think

using x = system.io;

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

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#

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

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

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);


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

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

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.


    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)
      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.

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

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,

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

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,

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

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close