Search code examples
c#stringfilestream

Replace the text in a file, but faster


So I made a math problem program that basically reads one number from a text file (only number in that text file) and replaces it with a number+1 if number is not a solution. Now the issue is, if I only add a text in the next row using

sw.WriteLine(text);

that makes the calculations really fast, doing 100k+ numbers in a few seconds, but it's just adding the number to the text file without deleting previous.

Alternatively I used

 string[] lines = File.ReadAllLines("numbers.txt");
 foreach (string line in lines)
 {
      lines[0] = Convert.ToString(biginta);
 }                     
            File.WriteAllLines("numbers.txt", lines);
            

but that made my program run considerably slower.

Is there a way I can replace text in a .txt file by using already open filestream?

I'm new to c# so my whole program is basically a Frankenstein of a code.


Solution

  • I'm using a file to store the next number needed to run because I turn off my pc overnight.

    Honestly the quickest solutiion to this is the following: Read the file once, do several (like 100) calculations without saving and then store the current number back into the file. Tune the interval so that you store the current state once every 5 seconds or so.

    That gives you still a good starting point (at most 5 seconds lost work) but also reduces disk IO to the point where it won't slow down the calculation any more.