[fpc-pascal] FreePascal Windows - Force files to write to disk

Santiago A. svaa at ciberpiula.net
Wed Feb 22 17:06:47 CET 2017

El 22/02/2017 a las 13:17, James Richters escribió:

> The error I get is when trying to open the file to read it.  It’s
> something like read past end of file, because the corrupted file is
> just one long line, once I read that, the second read is past the end
> of file.  I can do checking to get the file length and avoid the
> error, but that doesn’t solve the real issue, which is that the data
> that is supposed to be in the file is just gone. 

Just two lines of research ;-)

A) While you are reading the file you call the procedure that writes it.

B) The is a bug, not in writing the part, but in the reading code:
Somewhere when you read the file, you don't close it, so the file
remains open. Usually it's not a big issue, you can open the file
several times, and when you close the program everything is closed. But
some times, when a parameter is changed you rewrite it while it is still
open and everything gets messed. So this two events must happen, the
program has executed the part of code that lets the file open and next a
parameter is changed.

> The thing is, even if windows forced a restart while my program was
> running this file should have been closed at the time, because if
> someone was standing there editing the variables, they would see the
> restart notification and close the program first, or be able to tell
> me they had a power failure.
> As a temporary measure, I’m just writing out the file twice so at
> least I have a backup, but I don’t see why the backup would not be
> corrupted by this same issue, so that’s probably pretty useless.
Yes, I think that's the way to go.

1) Before writing, read it and if it's ok make a backup of the file
2) Write the file.
3) Read again what you have written to check whether it's ok.
4) Make a second backup.

That way, when you load parameters you have two backups to recover. And
maybe a clue of what's going wrong.
Beside, I would add a timestamp inside the file.

In addition, you could use flush and {$I-}


Santiago A.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20170222/74482703/attachment.html>

More information about the fpc-pascal mailing list