473,505 Members | 14,252 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

StreamReader read last line in a text file

Hi:

I want to check my text file to ensure the last line has only DONE on it,
prior to loading the file.

How can I do this with a StreamReader object to go to the last line so I can
perform the string comparison?

Thanks
Nov 17 '05 #1
2 33885
If i understand you correctly i would do it like this:

StreamReader f = new StreamReader(fileName);
ArrayList lines = new ArrayList();

string line;

while ((line = f.ReadLine()) != null )

{

lines.Add(line);

}

f.Close();

Then you move to the last index in ArrayList and tests for the word DONE

Best regads

Trond

"Tarren" <no***********@thanks.com> wrote in message
news:uG**************@TK2MSFTNGP15.phx.gbl...
Hi:

I want to check my text file to ensure the last line has only DONE on it,
prior to loading the file.

How can I do this with a StreamReader object to go to the last line so I
can perform the string comparison?

Thanks

Nov 17 '05 #2
Hi Tarren,

Besides reading all the info like Trond said, if you know that there is nothing after DONE (like line breaks or tabs etc) you could set the StreamReader's position to last position -4

using(FileStream fs = File.OpenRead("c:\\file.dat"))
{
using(StreamReader sr = new StreamReader(fs))
{
sr.BaseStream.Position = fs.Length - 4;
if(sr.ReadToEnd() == "DONE")
// match
}
}
On Mon, 25 Apr 2005 17:00:01 +0200, Tarren <no***********@thanks.com> wrote:
Hi:

I want to check my text file to ensure the last line has only DONE on it,
prior to loading the file.

How can I do this with a StreamReader object to go to the last line so I can
perform the string comparison?

Thanks


--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3944
by: BlueOysterCult | last post by:
Hello all I have a project that I have to read the file - which I have done I can see the file output. I then need to read the line - parse the line into three separate values (there are 3 = name...
9
4568
by: ShadowOfTheBeast | last post by:
Hi, I have got a major headache understanding streamReader and streamWriter relationship. I know how to use the streamreader and streamwriter independently. but how do you write out using the...
3
4983
by: Arno | last post by:
Hi, I'm using TcpClient for communication between two PC running a small piece of software. The protocol used has been designed internally and is HTTP similar (command line, headers, body). A...
13
3936
by: mloichate | last post by:
I must read a very heavy-weight text plain file (usually .txt extension) )and replace a given character with another given character in all text inside the file. My application was working pretty...
7
22381
by: Eric | last post by:
I am trying to save the "last read" position of a file using a StreamReader object. I am reading the lines of the file using StreamReader.ReadLine and then saving the current position in the...
2
4168
by: James Wong | last post by:
Dear all, I'm using StreamReader to read a text file containing BIG-5 data and found that no matter which encoding method in StreamReader's construction parameter, the BIG-5 contents become...
4
3122
by: KenLee | last post by:
help!! I used StreamReader and StreamWrite. the problem is it doesn't write all readline. For example it read 100 line and write 51lines. this is codes. class kenlee{ private StreamWriter sw...
1
5769
by: Arpan | last post by:
The contents of a text file are as follows: The Quick Brown Fox Jumped Over The Lazy Dog. Note that there isn't any space at the end of each of the 3 lines. Now when I do this:
4
23017
by: moondaddy | last post by:
I need to edit the text in many files so I'm writing a small routine to do this. First I have a method that loops through all the files in a directory and passes the full file path to another...
0
7216
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7303
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7018
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5613
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4699
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1528
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.