How to overwrite specific lines on text files
I have two text files. I'd like to copy a specific part in the first text file and replace it with a part of the second text file.
This is how I read the files:
List<string> PrevEp = File.ReadAllLines(string.Format(@"{0}naruto{1}.ass", url, PrevEpNum)).ToList();
List<string> Ep = File.ReadAllLines(string.Format(@"{0}naruto{1}.ass", url, EpNum))开发者_Python百科.ToList();
The part in PrevEp
that I need: from the start until it meets a line that includes Creditw,,0000,0000,0000
.
The part I would like to overwrite in Ep
: from the start to a line which is exactly Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
.
I'm not so sure how may I do it. Could you lend me a hand? Thank you very much.
Don't think of it as overwriting lines in a file, just do as you do at the moment and load it all into List
s and then you loop through Ep until you find the section you need, delete all the items from the list that you want to overwrite and the just keep track of the index and Insert
the new lines into that index.
Then you write Ep to a temporary file, delete the original Ep file and rename the temporary Ep file to the original name.
It's much easier to do the manipulation in memory in nice structures than in the files, and if you have a power failure or something you won't corrupt the file with half written edits.
I would read both text files, manipulate and rewrite them instead of trying to replace and do all kinds of trickery and sorcery.
Is your problem with finding the lines, or putting together the final file?
精彩评论