How to update a text file

G

Guest

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
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


DavidE said:
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.

You create a temp file, copy the unchanged lines without modification,
modify & write the one you need to modify and then copy back the temp file
to the real one.
 
M

Mark Chambers

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.

Create a new temporary file and write everything to that instead. On
successful completion, replace the original. It's easier and safer (i.e., if
something goes wrong, delete the temporary and the original remains intact).
 
A

Alberto Poblacion

DavidE said:
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.

If the lines you are updating do not change in length, you can update
them using a FileStream, which has Read() and Write() functions that can
read and write any number of bytes at any offset in the file. Basically, you
would be treating the file as a binary file instead of a text file.
However, if you are replacing arbitrary length lines, the only easy
solution is to create another file and write it anew. You cannot simply
replace some lines in the middle of the existing file because the different
length of the lines would cause all the rest of the file to "move" up or
down, so you would have to rewrite it on disk anyway.
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
G

Guest

Hi,

Thanks You all for your help. I realy appreciate it. Now I have a good
direction to solve this problem.

Have a good week .

David


Nicholas Paldino said:
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]
- (e-mail address removed)

DavidE said:
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
 

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