November 28, 2011, 11:21 AM
Help with C# and Text File
I am helping a friend with a project. He is currently having trouble deleting information from the text file, He is able to add and update information just fine but deleting is the prob. Anyone has any suggestions?
November 29, 2011, 01:58 PM
I have an algorithm he can try.
Imagine that the file is divided in three parts.
1. The text before the text to be deleted (let's call this text BEFORE).
2. The text to be deleted (let's call this text TO-BE-DELETED).
3. The text after the text to be deleted (let's call this text AFTER).
So the text file looks like this
Of course, this division is imaginary, I only use it so I can describe the algorithm easily.
The algorithm is:
1. Update the text that starts from TO-BE-DELETED with a copy of the text from AFTER.
2. Truncate the file after the updated text.
This looks like a student project so I don't know how solid you want to make this but you might want to consider the possibility that any (and maybe all?) parts of the file could be empty. For example, the text to be deleted could be at the end of the file (in which case AFTER is empty).
You might also want to be aware that if AFTER is longer than TO-BE-DELETED then the update is going to run into AFTER.