Substituting a string in a file

  • Thread starter Thread starter Daniele
  • Start date Start date
D

Daniele

How is it possible to quickly substitute a string in a text file?

Thanks,
Daniele
 
Daniele,

It probably would not be horribly efficient. Your best bet would be to
cycle through the file, byte by byte (or every two bytes, reading each
character). While doing this, you have to write the bytes to a temp file.
When you come across a byte or character that matches what you want to
replace (or the first character of such a string of bytes or characters),
you have to scan through, checking to see that the next n (where n is the
length of the string to be replaced) characters match. If they do, then you
write the replacement string to the new file.

Then, delete the original file, and move the new file to have the same
name as the old file.

Hope this helps.
 
Any idea about how do databases make changes in files when we issue an
update query?

Ab.

Nicholas Paldino said:
Daniele,

It probably would not be horribly efficient. Your best bet would be to
cycle through the file, byte by byte (or every two bytes, reading each
character). While doing this, you have to write the bytes to a temp file.
When you come across a byte or character that matches what you want to
replace (or the first character of such a string of bytes or characters),
you have to scan through, checking to see that the next n (where n is the
length of the string to be replaced) characters match. If they do, then you
write the replacement string to the new file.

Then, delete the original file, and move the new file to have the same
name as the old file.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Daniele said:
How is it possible to quickly substitute a string in a text file?

Thanks,
Daniele
 

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

Back
Top