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

James Richters james at productionautomation.net
Wed Feb 22 13:17:23 CET 2017

Thanks for the advice on things to look at. 


Here is some more information.  First of all, this happens extremely rarely, about once a month or two on a pc here and another there, but it’s odd that it’s always this one file, and it should not be happening at all.    I’m using turbo pascal compatibility mode, and it’s a console application, there is no multi threading going on, it’s straight inline code.  No other programs on the system ever access this file and there is no reason to open it with a text editor or anything like that.    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.  


Bitfile is type TEXT like this: 


Bitfile: Text;


and my variables are all doubles.  

Normally what happens is, everything is fine for a month or two, but then one day someone will come in and notice the error on the screen, it was fine the night before, the program running overnight, in the morning the program is not running,  and when the attempt is made to run it,  The error is found and the cause of the error is BitSave.pax has been corrupted and is now a long string of [nul]   the computers this runs on are all windows 10 with Samsung solid state hard drives.    I suspect that the majority of the time, this has happened due to winders doing an automatic update and restarting without permission as windows 10 annoyingly likes to do, thus crashing my program in order to do this restart that is SO important.  I have shut off windows update via group policy, but still had the problem since, however I cannot be certain there was not a power failure.   The computers are set to re-power up after power loss in BIOS and the program is set to run on startup, in fact the computers have no other function than to run this one program, they are never used for anything else.  The procedure that writes out this file is only ever used when one of the variables is changed, and they can only be changed by manual input, which is not happening anymore when the issue occurs.   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.

Unfortunately this is difficult to troubleshoot because it happens so rarely. I have tried to force it to happen by terminating my program with task manager and cannot cause the file to be corrupted.   I have not yet tried to duplicate the error by shutting down the system while the program is running.  I could try that.     If it does turn out to be an issue with my program running during shutdown,  is there some way I can detect a pending shutdown and exit normally before the shutdown happens?   It wouldn’t really solve the cause of the issue, but it would possibly help prevent some of the occurrences related to shutdowns, but would not help with power failures.


I am unable to use Try in turbo pascal compatible mode.  If I use it, I get identifier not found ‘Try’   If I attempt to compile in some mode other than turbo pascal compatible, I end up with thousands of other errors, so I would pretty much have to do a complete re-write.   I can’t see how the close could not be run, there is no way out of my procedure without running it since it’s a single thread console application.





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 3:48 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 21/02/2017 a las 22:12, James Richters escribió:


Thanks for any advice on this

My first action would be to guarantee that the file is closed with a try...finally.









Do you have the file opened with an editor, or things like that, while running the program?.


Check that all variables have valid values and that values are converted to string properly to be printable.


Do you use somewhere {$I+} {$I-}? I also had some problems with that with old programs.


What's the type BitFile? 
BitFile: TextFile;
BitFile: File;

Maybe using writeln with non-textfile files may cause problems.

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

More information about the fpc-pascal mailing list