If it has to be old school, I would write it like so:
program wvd;
Procedure WriteVillagedata; // This will write the Data Structure to file.
Var
e:Integer;
FilenameOut: textfile;
Begin
Assign(FilenameOut, 'VillageData.txt');
{$push}{$I- because we want to handle IOResult ourselves. This can be local}
ReWrite(FilenameOut);
e:= IOResult;// because IOResult gets cleared after access, store it!!
{$pop}
if e = 0 then Try
writeln(FilenameOut, 'text');
finally
flush(FilenameOut);
close(FilenameOut);
end else writeln ('error: ', e);
end;
begin
WriteVillagedata;
end.
I suspect that when examining IOResult the original code will show an error...
Also, just to be sure, call flush() before closing.
Anyway, if you pull in classes better to use a TFileStream and if you pull in sysutils, use exception objects.
Assign() - or the hidious AssignFile(), which is mode dependent - does not cause IOResult to be set, but rewrite() will... And now we handle 103 the proper way.
Errors are here:
https://www.freepascal.org/docs-html/rtl/system/ioresult.htmlNote, of course Bogen85's example code on which I based this, also works in most cases, I just added error handling the olden way.