Updating text file (without deleting/recreating)

  • Thread starter Thread starter Jeffrey
  • Start date Start date
J

Jeffrey

What classes/methods can I use to do the following?

1. Open a text file (less than 50k in size)
2. Replace some text in it.
3. Close the text file.

I don't want to have to create a new file in order to accomplish this (i.e.,
I don't want to open it, read to memory, do replacement in memory, delete
original file, write to new file from memory).

Thanks!
 
Jeffrey said:
What classes/methods can I use to do the following?

1. Open a text file (less than 50k in size)
2. Replace some text in it.
3. Close the text file.

I don't want to have to create a new file in order to accomplish this (i.e.,
I don't want to open it, read to memory, do replacement in memory, delete
original file, write to new file from memory).

Thanks!

Take a look at the BufferedStream class, which I think will do what you
want. The Seek method will allow you to go to a given position and then
you can write to the file with Write.

If you are looking for specific text in the file, you are going to need
to either know exactly where it is (offset) or find it by reading until
you find it.

matt
 
Jeff,

I think the only way you could accomplish this is in case the length of the
text you want to write equals the length of the text you want to replace.
Then you could treat the file as a stream of characters and Seek() into the
desired position in order to replace the text.
See:

http://msdn.microsoft.com/library/d...tml/frlrfsystemiofilestreamclassseektopic.asp

Being such small files (<50K) I think the "standard" approach (read from a
file and write into another) could work very fast.

Regards - Octavio
 
Back
Top