Use both read and write operation to the same file

C

Curious

I have a file to which I'll need to at first read until it locates the
last line of string.

According to content of the last line of string, I'll need to write
something to this file right after the last line of string.

This requires that I'll need a StreamReader at first (to read one line
at a time), until the last line is read.

Then I'll need a StreamWriter to point to the end of the file, and
write to it.

Questions:

1) Is there a single Stream type of object that I can use for both
reading and writing, because I'll need to write at the point where
read operation is done?

2) Is there a method in the Stream object that reads only the last
line from a file (instead of reading all of the lines of strings)?
 
F

Family Tree Mike

Unless the file size is large, I think the easiest way would be the following:

string [] lines = System.IO.File.ReadAllLines("somefile.txt");
// do some manipulations with lines[lines.Count - 1];
System.IO.File.WriteAllLines("somefile.txt", lines);
 
C

Curious

Good to know that there is a way to get all of the lines in a single
operation, and get the last line!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top