Solved

Get C# File "Stream Reader" Last Line

Posted on 2012-03-30
7
1,773 Views
Last Modified: 2012-06-21
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
Comment
Question by:AhmedHindy
7 Comments
 
LVL 9

Expert Comment

by:OCDan
ID: 37789487
String whatever= new String;
while !(EOF)
{
  whatever = readline code here
}
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37789492
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
 
LVL 27

Accepted Solution

by:
Chinmay Patel earned 300 total points
ID: 37789590
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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 4

Author Comment

by:AhmedHindy
ID: 37789592
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
 
LVL 27

Expert Comment

by:Chinmay Patel
ID: 37789599
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 37789627
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
 
LVL 4

Author Comment

by:AhmedHindy
ID: 37790634
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Entity Framework 7 43
In C# Using WebClient method how to post certificate from file with password 3 34
Install Problem 13 32
Hey!! 5 33
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to dynamically set the form action using jQuery.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question