Text File Insert and Delete Capabilities
Posted on 1997-09-07
Is there a way to insert and delete data in an open text file without having to rewrite the entire text file.
Consider for example that I've open a text file, located a specific string within the text file (line, position and length) and need to either remove the selected string from the file or replace it with another string.
The method I'm currently using to (for example) replace a string seems very inefficient:
1. Locate the string (search method)
2. Read and store all data upto the string of interest in array 1.
3. Read and store all data following the string of interest in array 2.
4. Create a temp file
5. Write all data from array 1 to the temp file, write the replacement string to the temp file, then write all data in array 2 to the temp file.
6. Destroy the original file.
7. Rename the temp file to the original file name.
This process if very cumbersom especially if I only need to change one character in a 5000 line text file. There has to be a better way.