Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Get C# File "Stream Reader" Last Line

Posted on 2012-03-30
Medium Priority
2,331 Views
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
Question by:AhmedHindy
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 9

Expert Comment

ID: 37789487
String whatever= new String;
while !(EOF)
{
}
0

LVL 75

Expert Comment

ID: 37789492
Like this?

string lastLine = string.Empty;

{
{
}
}

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

0

LVL 27

Accepted Solution

Chinmay Patel earned 900 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

LVL 4

Author Comment

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

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 86

Expert Comment

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

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

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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…
###### Suggested Courses
Course of the Month5 days, 9 hours left to enroll