-3

I need to edit a big text file (around 100MB). What I need to do is find some texts, and replace it by another one. I saw many similar questions as that one, but I guess there would be memory problems to load all the lines in memory? Is there a way to do the job editing the lines one by one? What would be the best approach for that?

Siegfried.V
  • 886
  • 1
  • 9
  • 23
  • Does this answer your question? [Edit a specific Line of a Text File in C#](https://stackoverflow.com/questions/1971008/edit-a-specific-line-of-a-text-file-in-c-sharp) (specifically the second part of the accepted answer) – Franz Gleichmann Feb 08 '21 at 11:25
  • @FranzGleichmann yes thanks, it seems to answer to my question, just testing remaining :) – Siegfried.V Feb 08 '21 at 11:28
  • _I guess there would be memory problems_ Did you even try? 100MB is not really big. – TaW Feb 08 '21 at 12:06
  • @TaW I am still developing using solution advised by Franz, didn't finish yet (I think today will be done). 100MB is my example, I guess I can get even 1Gb files or more. The question was if there was a way to edit line by line, but as I read on other similar questions, this is not possible. – Siegfried.V Feb 09 '21 at 07:11

1 Answers1

0

I would do the following (pseudocode):

Open source file
Create target file
for each line in source file
   target line = result of replacing some text in line
   write target line into target file
end for
close source and target files
replace source file with target file

In this way you don't need much memory.

Jesús López
  • 6,739
  • 2
  • 26
  • 55