Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2017-07

Fixed record files

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

James Richters

I have a few questions about fixed record files. I have the following:

Type
   DrawCMD = Record
                GC: Byte;
                PX,PY,PZ,GX,GY,GZ,GI,GJ,R,SA,EA:Double;
             End;
Var
  DrawFile                    : File Of DrawCMD;
  Drawinfo                    : DrawCMD;

I wish to get to the end of the file and read the last record. Currently I am doing it like this:

         Reset(DrawFile);
         while not eof(DrawFile) do
            begin
               Read(DrawFile,DrawInfo);
            End;

When this finishes Drawinfo contains the last record of the file. But I'm wondering if there is a better way since it's a file of fixed records.

I'm also wondering if there is a way to insert a record at the beginning of the file, or in the middle of the file without doing something like copying the entire file to a temp file, then re-writing the entire new file in the desired order?

James

Reply by email

Ched
to James Richters

Hello,

I'll try something like

If filesize(drawfile)>0 then
    Seek(drawfile, sizeof(drawfile)-1)
else
    "empty file";

For portability of you file and software, I recommand you to use a "packed record" with an internal forced word-align of the double data. If size matters, no need to align.

Type
      DrawCMD = Record
                   GC: Byte;
                   ______: byte; { no use axcept to force align on words for speed on certain systems }
                   PX,PY,PZ,GX,GY,GZ,GI,GJ,R,SA,EA:Double;
                End;

This can also be uses for memory access, as on some processors, word aligning data can save some cpu cycles with reference to non aligned data for data longer than one byte/char.

Chers, Ched

James Richters wrote 45 lines
I have a few questions about fixed record files. I have the following: Type DrawCMD = Record GC: Byte; PX,PY,PZ,GX,GY,GZ,GI,GJ,R,SA,EA:Double; End; Var DrawFile : File Of DrawCMD; Drawinfo : DrawCMD; I wish to get to the end of the file and read the last record. Currently I am doing it like this: Reset(DrawFile); while not eof(DrawFile) do begin Read(DrawFile,DrawInfo); End; When this finishes Drawinfo contains the last record of the file. But I’m wondering if there is a better way since it’s a file of fixed records. I’m also wondering if there is a way to insert a record at the beginning of the file, or in the middle of the file without doing something like copying the entire file to a temp file, then re-writing the entire new file in the desired order? James _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

James Richters
to Ched

I prefer performance over size, The only reason 'GC' was a byte was because it didn't really need to be a word, however it wouldn't hut to be a word anyway, so I could just use this:

Type
      DrawCMD = Record
                   GC: Word;
                   PX,PY,PZ,GX,GY,GZ,GI,GJ,R,SA,EA:Double;
                End;

I don't understand "packed record" could you elaborate?

James

Earlier message 81 lines
-----Original Message----- From: fpc-pascal On Behalf Of Ched Sent: Sunday, July 09, 2017 12:01 PM To: FPC-Pascal users discussions Subject: Re: [fpc-pascal] Fixed record files Hello, I'll try something like If filesize(drawfile)>0 then Seek(drawfile, sizeof(drawfile)-1) else "empty file"; For portability of you file and software, I recommand you to use a "packed record" with an internal forced word-align of the double data. If size matters, no need to align. Type DrawCMD = Record GC: Byte; ______: byte; { no use axcept to force align on words for speed on certain systems } PX,PY,PZ,GX,GY,GZ,GI,GJ,R,SA,EA:Double; End; This can also be uses for memory access, as on some processors, word aligning data can save some cpu cycles with reference to non aligned data for data longer than one byte/char. Chers, Ched Le 09.07.2017 à 13:45, James Richters a écrit : > I have a few questions about fixed record files. I have the following: > > Type > > DrawCMD = Record > > GC: Byte; > > PX,PY,PZ,GX,GY,GZ,GI,GJ,R,SA,EA:Double; > > End; > > Var > > DrawFile : File Of DrawCMD; > > Drawinfo : DrawCMD; > > I wish to get to the end of the file and read the last record. Currently I am doing it like this: > > Reset(DrawFile); > > while not eof(DrawFile) do > > begin > > Read(DrawFile,DrawInfo); > > End; > > When this finishes Drawinfo contains the last record of the file. But > I’m wondering if there is a better way since it’s a file of fixed records. > > I’m also wondering if there is a way to insert a record at the > beginning of the file, or in the middle of the file without doing > something like copying the entire file to a temp file, then re-writing the entire new file in the desired order? > > James > > > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal >

Reply by email

Vojtěch Čihák
to Ched: I'll try something like If filesize(drawfile)>0 then Seek(drawfile, sizeof(drawfile)-1)…

Hi,
 
"packed record" is for compatibility. Its guaranteed that inner alignment and size of record will be always the same and will not change in future versions of compiler, unlike the plain "record".  
https://www.freepascal.org/docs-html/ref/refsu15.html  
V.
______________________________________________________________

Show quoted text 5 lines
Od: "James Richters" Komu: "'FPC-Pascal users discussions'" Datum: 09.07.2017 20:12 Předmět: Re: [fpc-pascal] Fixed record files

I don't understand "packed record" could you elaborate?

James

Reply by email

James Richters
to Vojtěch Čihák

Thanks for the link. For best compatibility should I make it:

Type
      DrawCMD = Packed Record
                   GC: Word;
                   PX,PY,PZ,GX,GY,GZ,GI,GJ,R,SA,EA:Double;
                End;

Or

Type
     {$PackRecords 2}
      DrawCMD =  Record
                   GC: Word;
                   PX,PY,PZ,GX,GY,GZ,GI,GJ,R,SA,EA:Double;
                End;
     {$PackRecords default}

Or am I not understanding this correctly?

James

Earlier message 20 lines
From: fpc-pascal On Behalf Of Vojtech Cihák Sent: Sunday, July 09, 2017 2:44 PM To: FPC-Pascal users discussions Subject: Re: [fpc-pascal] Fixed record files Hi, "packed record" is for compatibility. Its guaranteed that inner alignment and size of record will be always the same and will not change in future versions of compiler, unlike the plain "record". https://www.freepascal.org/docs-html/ref/refsu15.html V.

Reply by email