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

James Richters james at productionautomation.net
Wed Feb 22 21:27:55 CET 2017


Here is the procedure that reads the file. It is only read once when the program is started, then written to when changes are made.

I don’t have a good reason for having a different file variable name in my read procedure than my write procedure, but those are local varaibles to the procedures anyway so it shouldn’t matter what I call them.

 

Var

   locfile : text;

If FileExists('Bitsave.pax') Then

Begin

   Writeln('BitSave.pax Found, Loading... ');

   Assign(locfile,'BitSave.pax');

   reset(locfile);

   ReadLn(locfile,XAdj);

   ReadLn(locfile,YAdj);

   ReadLn(locfile,ZAdj);

   ReadLn(locfile,WAdj);

   ReadLn(locfile,AAdj);

   ReadLn(locfile,TAdj);

   ReadLn(locfile,VAdj);

   ReadLn(locfile,UAdj);

   ReadLn(locfile,CurrentTool);

   ReadLn(locfile,P_Value[4]);

   Close(locfile);

End

 

I will do as you suggest and read what I just thought I wrote back to verify it.   I may also incorporate some kind of checksum as well and it wouldn’t be a bad idea to check how many lines are in the file before I try to read it, just to make sure I don’t get read past end of file error.  It seems like a lot just to save just 10 numbers, but it’s a pain if those numbers are gone!    I will also try flush and {$i-}  as well.  

 

Thanks to everyone for your advice and help

 

Jim

 

From: fpc-pascal-bounces at lists.freepascal.org [mailto:fpc-pascal-bounces at lists.freepascal.org] On Behalf Of Santiago A.
Sent: Wednesday, February 22, 2017 11:07 AM
To: FPC-Pascal users discussions <fpc-pascal at lists.freepascal.org>
Subject: Re: [fpc-pascal] FreePascal Windows - Force files to write to disk

 

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-}





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


More information about the fpc-pascal mailing list