Recent

Author Topic: EOF Problem  (Read 504 times)

J-G

  • Hero Member
  • *****
  • Posts: 966
EOF Problem
« on: April 27, 2025, 08:29:12 pm »
As you'll have gathered, I'm not doing much coding at the moment but the 'problem' that I asked about in January has reared its head.

A reminder - I have an old customer with a very old PC which 'Died' taking his Garage business database out.

It was running a DOS program so there was no Mouse support and writing a Windows program was not viable - even though I did go through the motions. He brought me a 'backup' PC  which was running Win 98 but didn't have a COM Port mouse, No PS/2 port . . . . . Long Story  -  now short . . . .   I've at least 'Resurected' it (I've actually named it 'Lazarus'! %) )  Installed a PCI card with PS/2 & USB along with a copy of Win XP  -  It's now fully operational and I have run the Pascal program on it.

NOW  -  He was hoping to re-install his old program probably running in a DOS Window but the 'Master' discs that he has are only a [Demo] version. So I offered to extract his 'Products' file from the Backup Data that he did have - (I'd already extracted Names etc. & Vehicles) This was going very well but out of the blue I'm getting a 103 File not Open error  -  this is with the Name & Address File which had been working perfectly  -  -  Stepping through, I can see that the file 'Opens' -  ie. IOResult returns zero,  but when it should then read the file until EOF it skips to 'Close File' (jumps from line 5 to 12) and returns the 103 when I try to execute line 12.

Two bits of code -  the first is the assignment and opening and the second is the Reading :
Code: Pascal  [Select][+][-]
  1.   Assign(NAD_FIle,datapath+'LeekNAD.DAT');
  2.   {$I-} Reset(NAD_file); {$I+}
  3.   I := IOResult;
  4.   If I = 0
  5.   then
  6.     begin
  7.       Read_NAD_File;
  8.     end
  9.   else
  10.  
Code: Pascal  [Select][+][-]
  1.   procedure Read_NAD_File;
  2.   begin
  3.     SetLength(NAD,20);
  4.     n :=1;
  5.     while not EOF do
  6.       begin
  7.         read(NAD_File,NAD[n]);
  8.         inc(n);
  9.         if n > length(NAD)-1
  10.           then SetLength(NAD,Length(NAD)+20);
  11.       end;
  12.     close(NAD_File);
  13.   end;
  14.  

Nothing extraordinary and I've just checked the Vehicles file and that's doing the same !!!!!    Both .DAT files have been in use since they were created in January and I've also re-created them from the original .DBF files.

Can anyone suggest what I might have done to upset the applecart?
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

MarkMLl

  • Hero Member
  • *****
  • Posts: 8388
Re: EOF Problem
« Reply #1 on: April 27, 2025, 08:38:50 pm »
You either need to reset the file with an explicit record length, or declare it as "file of" an appropriately-declared record, or if you've told Reset() that the record length is 1 byte then use BlockRead() with an appropriate count parameter.

By default, the record size is documented as 128 bytes which is a hangover from CP/M-80 days.

https://www.freepascal.org/docs-html/current/rtl/system/filefunctions.html

If you really do know the format, defining a record of that type then double-checking its size and using "file of" would be the preferred "old school" way of doing it.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

J-G

  • Hero Member
  • *****
  • Posts: 966
Re: EOF Problem
« Reply #2 on: April 27, 2025, 08:49:23 pm »
Thanks for the swift response Mark but I think you've missed the point that these files have been working perfectly for quite some time.

The variables in use are
Code: Pascal  [Select][+][-]
  1.   NAD_File   : File of Customer;
  2.   NAD        : Array of Customer;
  3.   NAD_Length : Word;
  4.  
  5.   VEH_File   : File of Vehicle;
  6.   VEH        : Array of Vehicle;
  7.   VEH_Length : Word;
  8.  
  9.   PROD_File  : File of Product;
  10.   PROD       : Array of Product;
  11.   PROD_Length: Word;
  12.  

with the 'Customer', 'Vehicle' & 'Product' all being fully defined 'Records'.

Yes I do know the precise structure of the .DBF files as well.

I'm just adding the [Product] to what was a fully operational program and I haven't modified any of the code that reads (or rather doesn't!) the Customer or Vehicle files.

« Last Edit: April 27, 2025, 09:09:43 pm by J-G »
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

jamie

  • Hero Member
  • *****
  • Posts: 6888
Re: EOF Problem
« Reply #3 on: April 27, 2025, 09:01:52 pm »
yours EOF check isn't checking the file handle, its defaulting to keyboard in etc.

you need EOF(NAD_File) ,,,,,

The only true wisdom is knowing you know nothing

J-G

  • Hero Member
  • *****
  • Posts: 966
Re: EOF Problem
« Reply #4 on: April 27, 2025, 09:20:05 pm »
yours EOF check isn't checking the file handle, its defaulting to keyboard in etc.

you need EOF(NAD_File) ,,,,,

Jamie  !!!  -  Thanks     I knew it just needed a decerning 'Eye' over it  :D

How it has worked for the past months is beyond my comprehension but I'm sure that there is likely to be a rational explanation.

I can't even fathom 'when' it changed or what I did - it worked this morning - - -   and is now working again. I'm very grateful.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

ASerge

  • Hero Member
  • *****
  • Posts: 2408
Re: EOF Problem
« Reply #5 on: April 28, 2025, 08:57:30 pm »
...and is now working again.
And this?
Code: Pascal  [Select][+][-]
  1.  SetLength(NAD,20);
  2.     n :=1; // Must be zero !!!
  3.     while not EOF(NAD_File) do
  4.       begin
  5.         read(NAD_File,NAD[n]);  
  6.         inc(n);
  7.         if n > length(NAD)-1 // better n >= length(NAD)
  8.           then SetLength(NAD,Length(NAD)+20);

OC DelGuy

  • Full Member
  • ***
  • Posts: 208
  • 123
Re: EOF Problem
« Reply #6 on: April 28, 2025, 09:38:57 pm »
yours EOF check isn't checking the file handle, its defaulting to keyboard in etc.

you need EOF(NAD_File) ,,,,,

I saw that!  I don't have anything to add, but I'm proud of myself that I actually saw the problem and knew the fix!  :o  :D

Even the little things excite me!



Having said that...
How it has worked for the past months is beyond my comprehension but I'm sure that there is likely to be a rational explanation.

I can't even fathom 'when' it changed or what I did - it worked this morning - - -   and is now working again. I'm very grateful.

What DID change?  Why did it work for months and then...  Not?  :o   (Assuming no one changed the program.)

Just curious
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

 

TinyPortal © 2005-2018