I have an algorithm that takes 7 days to Run To Completion (and few more algorithms too)
Problem: In order to successfully Run the program, I need continuous power supply. And if out of luck, there is a power loss in the middle, I need to restart it again.
So I would like to ask a way using which I can make my program execute in phases (say each phase generates Results A,B,C,…) and now in case of a power loss I can some how use this intermediate results and continue/Resume the Run from that point.
Problem 2: How will i prevent a file from re opening every time a loop iterates ( fopen was placed in a loop that runs nearly a million times , this was needed as the file is being changed with each iteration)
Problem 2: Opening the file on each iteration of the loop because it’s changed
I may not be best qualified to answer this but doing
fopenon each iteration (andfclose) presumably seems wasteful and slow. To answer, or have anyone more qualified answer, I think we’d need to know more about your data.For instance:
I ask as, judging by your comment “because it’s changed each iteration”, would you be better using a random-accessed file. By this, I’m guessing you’re re-opening to
fseekto a point that you may have passed (in your stream of data) and making a change. However, if you open a file as binary, you canfseekthrough anywhere in the file usingfsetposandfseek. That is, you can “seek” backwards.Additionally, if your data is record-based or somehow organised, you could also create an index for it. with this, you could use to
fsetposto set the pointer at the index you’re interested in and traverse. Thus, saving time in finding the area of data to change. You could even persist your index in an accompanying index file.Note that you can write plain text to a binary file. Perhaps worth investigating?