Recent

Author Topic: file Blockread error  (Read 3514 times)

TBMan

  • New Member
  • *
  • Posts: 47
file Blockread error
« on: January 13, 2025, 05:23:13 pm »
I'm doing this with an untyped file. The program is halting on the blockread. I have tested the file with other software and it is fine. Do I have to define a compiler directive or something similar?


Code: Pascal  [Select][+][-]
  1. type
  2.    
  3. boardItem = (blank, dot, horizontal, vertical);
  4.  
  5.  boardtype = record
  6.    x,y:integer;
  7.    I_am : boarditem;
  8. end;
  9.  
  10.  boardarray = array [1..31,1..28] of boardtype;
  11.  
  12. var
  13. f:file
  14. board:boardarray;
  15. begin
  16.  assign(f,'board.pac');
  17.  reset(f,1);
  18.  blockread(f,board,sizeof(board));
  19.  close(f);
  20. end.
  21.  

MarkMLl

  • Hero Member
  • *****
  • Posts: 8221
Re: file Blockread error
« Reply #1 on: January 13, 2025, 05:42:40 pm »
No, but you /do/ have to give us adequate information: starting with the precise error you're getting, FPC (and possibly Lazarus etc.) version, the compilation command you're using, the OS you're running under and so on.

Posting a compilable program plus test data is encouraged.

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

bytebites

  • Hero Member
  • *****
  • Posts: 698

TBMan

  • New Member
  • *
  • Posts: 47
Re: file Blockread error
« Reply #3 on: January 13, 2025, 06:11:26 pm »
https://www.freepascal.org/docs-html/rtl/system/blockread.html

Thanks. For some reason, the sizeof() in my program is returning double the size of the board. The board is 12152. Odd.

jamie

  • Hero Member
  • *****
  • Posts: 6801
Re: file Blockread error
« Reply #4 on: January 13, 2025, 06:17:26 pm »
File of byte
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 6801
Re: file Blockread error
« Reply #5 on: January 13, 2025, 06:57:02 pm »
If u r copying from 16 bit code and reading old files the integer type is 32 bit but use to be 16 bit.
Maybe that's your problem.
Jamie
The only true wisdom is knowing you know nothing

TBMan

  • New Member
  • *
  • Posts: 47
Re: file Blockread error
« Reply #6 on: January 13, 2025, 07:02:01 pm »
Ok, I found the problem.

In my old compiler, Borland Pascal, the "board" is 12,152 bytes. The same "board" using Lazarus is double the size.  So long story short, I had converted the
board to a text file which had 31 lines of text made up of characters that represent the board items. Then I read the file in Lazarus and converted the board back to the record type and saved that file. That file
is twice the size of the board saved by my old compiler running in dosbox.

The good thing is now I can take that text file that I created and convert it to a CONST array of 1..31 and keep it in a unit, instead of a file. If I want to distribute this program to friends I'll be able to without
problems.  I've already made a method of converting the board display sprites I made for the DOSBOX version of this application (a PacMan clone) by reading the sprite file and converting it to a unit of const arrays. I just have to do the conversion for the pacman, ghosts and bonus fruit. I should have a running demo in a few weeks, wife permitting, lol.

Thanks everyone for helping out!

TBMan

  • New Member
  • *
  • Posts: 47
Re: file Blockread error
« Reply #7 on: January 13, 2025, 07:02:54 pm »
If u r copying from 16 bit code and reading old files the integer type is 32 bit but use to be 16 bit.
Maybe that's your problem.
Jamie

Yep, I was posting while you were posting. That's it. That's why the file size doubled.

ASerge

  • Hero Member
  • *****
  • Posts: 2379
Re: file Blockread error
« Reply #8 on: January 14, 2025, 02:00:55 am »
Yep, I was posting while you were posting. That's it. That's why the file size doubled.
When records are used for file operations, always fix the size using specific types rather than generic ones.
Your example:
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2. {$IFDEF WINDOWS}
  3.   {$APPTYPE CONSOLE}
  4. {$ENDIF}
  5.  
  6. type
  7. {$PUSH}
  8. {$PACKENUM 1} // SizeOf(TBoardItem) alway 1 byte
  9.   TBoardItem = (biBlank, biDot, biHorizontal, biVertical);
  10. {$POP}
  11.  
  12.   TBoardRec = packed record // "packed" eliminates padding by the compiler
  13.     x, y: Int16; // SizeOf(x) always 2 bytes
  14.     I_am : TBoardItem;
  15.   end;
  16.  
  17.   // For now arrays are always packed, but this way it will be clear that this structure must have a fixed size.
  18.   TBoardArray = packed array [1..31, 1..28] of TBoardRec;
  19.  
  20. var
  21.   f: file;
  22.   Board: TBoardArray;
  23. begin
  24.   AssignFile(f, 'board.pac');
  25.   Reset(f, 1);
  26.   try
  27.     BlockRead(f, Board, SizeOf(Board));
  28.   finally
  29.     CloseFile(f);
  30.   end;
  31. end.


TBMan

  • New Member
  • *
  • Posts: 47
Re: file Blockread error
« Reply #9 on: January 14, 2025, 03:45:29 am »
Yep, I was posting while you were posting. That's it. That's why the file size doubled.
When records are used for file operations, always fix the size using specific types rather than generic ones.
Your example:
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2. {$IFDEF WINDOWS}
  3.   {$APPTYPE CONSOLE}
  4. {$ENDIF}
  5.  
  6. type
  7. {$PUSH}
  8. {$PACKENUM 1} // SizeOf(TBoardItem) alway 1 byte
  9.   TBoardItem = (biBlank, biDot, biHorizontal, biVertical);
  10. {$POP}
  11.  
  12.   TBoardRec = packed record // "packed" eliminates padding by the compiler
  13.     x, y: Int16; // SizeOf(x) always 2 bytes
  14.     I_am : TBoardItem;
  15.   end;
  16.  
  17.   // For now arrays are always packed, but this way it will be clear that this structure must have a fixed size.
  18.   TBoardArray = packed array [1..31, 1..28] of TBoardRec;
  19.  
  20. var
  21.   f: file;
  22.   Board: TBoardArray;
  23. begin
  24.   AssignFile(f, 'board.pac');
  25.   Reset(f, 1);
  26.   try
  27.     BlockRead(f, Board, SizeOf(Board));
  28.   finally
  29.     CloseFile(f);
  30.   end;
  31. end.

Thank you! Great info!

Thaddy

  • Hero Member
  • *****
  • Posts: 16532
  • Kallstadt seems a good place to evict Trump to.
Re: file Blockread error
« Reply #10 on: January 14, 2025, 11:53:53 am »
Why not simply
Code: Pascal  [Select][+][-]
  1. type
  2.    
  3. boardItem = (blank, dot, horizontal, vertical);
  4.  
  5. boardtype = record
  6.    x,y:integer;
  7.    I_am : boarditem;
  8. end;
  9.  
  10.  boardarray = array [1..31,1..28] of boardtype;
  11.  
  12. var
  13.   f:file of boardtype;
  14.   board:boardarray;
  15. begin
  16.  assign(f,'board.pac');
  17.  reset(f,1);
  18.  read(f,board);
  19.  close(f);
  20. end.
?
But I am sure they don't want the Trumps back...

 

TinyPortal © 2005-2018