How to delete between specific lines from text file in C#
Hello,
I've one text file and i'd like to delete lines between two specific lines then i'd like to add new lines between these two lines. I really need an expert support for correct approach how to do that.
If there will be 5 lines deleted, do you mean to replace it with ONE new line each time, or the same (in this case, 5) empty lines? Are you sure this file contains only spaces? Not tabs?
Bill Prew
I'm assuming you have some skills in C# since you are asking in that group.
Conceptually this feels fairly straight forward.
- open the input file for reading
- open the output file for writing
- read each line from input file, and write it to output file until you hit the "start" text match
- write the new lines you want to insert to output file
- continue reading the input file looking for the "end" text match, not writing them anywhere
- once you see the "end" text match, go back to reading lines from input and writing them to output
There are several ways you can implement this, setting a flag once you see "start" or "end", or a mode switch etc, or just moving on to different sections of logic and looping, that's often a personal preference.
Are you sure this file contains only spaces? Not tabs?