David,
You can't just update a few lines in the middle of a file. Rather, you
will have to write your changes at the appropriate point in the file, and
then write the information that you want to write after the changes back to
the file (for the rest of the file).
Say you wanted to insert the string "Hello" (ASCII characters) into the
file. You would have to go to the point you want to insert it, and read 10
bytes from the file, storing them in a buffer (twice the length of the
string you want to write). I'd suggest storing them in two arrays of five
bytes each. Then you write "Hello". Once you do that, you can write the
first five bytes of the buffer after hello. Then, read the next five bytes
in the file and store them in the buffer (this is why I recommend two
buffers, so you can just swap references as opposed to having to copy all
the bytes around in a single buffer). You then keep repeating this for the
rest of the file.
--
- Nicholas Paldino [.NET/C# MVP]
-
mv*@spam.guard.caspershouse.com
"DavidE" <Da****@discussions.microsoft.comwrote in message
news:C2**********************************@microsof t.com...
Hi,
I have to open a text file in c# and read few lines and then I need to
update some lines in the middle of the file. I can read and write by using
the streamReader or streamWriter but I don't know how to read for example
five lines and then update the sixth line and continou to read few lines,
update few etc.
Thanks
David