Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2680
  • Last Modified:

Get C# File "Stream Reader" Last Line

Hi ,
I need To Get The Last Line Only From A massive 10GB Text File  
Any Help Will Be Accepted And Thanks In Advance For The Effort
0
AhmedHindy
Asked:
AhmedHindy
1 Solution
 
OCDanCommented:
String whatever= new String;
while !(EOF)
{
  whatever = readline code here
}
0
 
käµfm³d 👽Commented:
Like this?

string lastLine = string.Empty;

using (System.IO.StreamReader reader = new System.IO.StreamReader("c:\\some\\path\\to\\file.txt"))
{
    while (!reader.EndOfStream)
    {
        lastLine = reader.ReadLine();
    }
}

// lastLine is now populate with last line of file or an empty string if no lines were read.

Open in new window

0
 
Chinmay PatelEnterprise ArchitectCommented:
Hi AhmedHindy,

I would not recommend reading the file forward till the end and then read the last line.

I see there are two alternatives here
1. Use Tail for Windows - and using Process.Start you launch the application and get the results.
2. First seek to the end of the file and then read backwards till you hit a new line.

This file has fixed length lines?

Regards,
Chinmay.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
AhmedHindyAuthor Commented:
Hi , Thanks For The Fast Answers But ,
My Main Problem is The File Length about 10 GB I need Some Fast Algorithm To Get Last Line Only Directly Or Almost Directly To Save Time
0
 
Chinmay PatelEnterprise ArchitectCommented:
Kindly note the URL for tail for the windows : http://sourceforge.net/projects/tailforwin32/

I posted the URL but it wouldn't work and I was editing my answer when you commented your last post :)

Kindly let me know if this file has fixed lenght lines or not?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Right...if the file does NOT have fixed width lines (as Chinmay_Patel has asked) then you have to read byte by byte backwards from the end of the file until you find your carriage return/line feed combination.

See: http://stackoverflow.com/questions/398378/get-last-10-lines-of-very-large-text-file-10gb-c-sharp
0
 
AhmedHindyAuthor Commented:
First Thanks For All Responses ,
Second  Unfortunately File Has No Fixed Length
And I Will Try To Read The File Backward
Thanks Again For The Effort
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now