Read last line of a text file
Anyone have any idea how to make a StreamReader read the last line of a text file without knowing what it is. For example if I have a text file like this:
how can I make my program read the last line which is "7" but with out me telling the program that it is seven? Any help is appreciated thanks :)
Re: Read last line of a text file
Maybe something like this:
int index = 1;
stream.Seek(0, SeekOrigin.End)
while(stream.ReadByte () != '\n')
{
stream.Seek(index, SeekOrigin.End)
++index
}
Re: Read last line of a text file
try this:
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace streamreader
{
class Program
{
static void Main(string[] args)
{
StreamReader st = new StreamReader("test.txt");
string str = st.ReadToEnd();
int x = str.LastIndexOf('\n');
string lastline = str.Substring(x+1);
Console.WriteLine(lastline);
Console.ReadLine();
}
}
}
Touraj Ebrahimi [toraj_e] [at] [yahoo] [dot] [com]