Board index » delphi » Delphi doesn't let me read files the way I want

Delphi doesn't let me read files the way I want

What is the de-facto method for handling typed files where the
record lengths and variables are unpredictable (i.e. dependent on
the content)?

For example, you can't define something like this.........

   TMyFileData = record
      header1 : integer;
      header2 : (( anywhere from 3 to 9 integers, specified by header1)) ;
      header3 : string[40];
      header4 : (( 2 real numbers ));
      etc...
   var MyFileData = TMyFileData;
   read(infile,MyFileData);

However I don't know of any way to just go in and do.........
     read(infile,header1);
     for n := 1 to header1 do read(infile,header2[n]);
     read(infile,header3);
     read(infile,header4);

as Delphi always wants a fixed structure of some kind.

Do I have to just read one byte at a time and assemble my variables
on-the-fly?

This seems a bit stupid as Perl is not fussy over reading whatever
type I want out of any file at any time.

MJ

 

Re:Delphi doesn't let me read files the way I want


just create a  FILE ..
Var
  F:FILE:
Begin
 AssignFile(F,'asdsda');
 Reset(F, 1); // Set record size to 1 byte.
 //
  now use the BlockWRite, BlockRead, Seek etc..
Quote
Mark Smigel wrote:
> What is the de-facto method for handling typed files where the
> record lengths and variables are unpredictable (i.e. dependent on
> the content)?

> For example, you can't define something like this.........

>    TMyFileData = record
>       header1 : integer;
>       header2 : (( anywhere from 3 to 9 integers, specified by header1)) ;
>       header3 : string[40];
>       header4 : (( 2 real numbers ));
>       etc...
>    var MyFileData = TMyFileData;
>    read(infile,MyFileData);

> However I don't know of any way to just go in and do.........
>      read(infile,header1);
>      for n := 1 to header1 do read(infile,header2[n]);
>      read(infile,header3);
>      read(infile,header4);

> as Delphi always wants a fixed structure of some kind.

> Do I have to just read one byte at a time and assemble my variables
> on-the-fly?

> This seems a bit stupid as Perl is not fussy over reading whatever
> type I want out of any file at any time.

> MJ

Re:Delphi doesn't let me read files the way I want


In article <59b1f153.0304101048.77c15...@posting.google.com>,

Quote
mjsmi...@hotmail.com (Mark Smigel) writes:
>What is the de-facto method for handling typed files where the
>record lengths and variables are unpredictable (i.e. dependent on
>the content)?

>For example, you can't define something like this.........

>   TMyFileData = record
>      header1 : integer;
>      header2 : (( anywhere from 3 to 9 integers, specified by header1)) ;
>      header3 : string[40];
>      header4 : (( 2 real numbers ));
>      etc...
>   var MyFileData = TMyFileData;
>   read(infile,MyFileData);

>However I don't know of any way to just go in and do.........
>     read(infile,header1);
>     for n := 1 to header1 do read(infile,header2[n]);
>     read(infile,header3);
>     read(infile,header4);

>as Delphi always wants a fixed structure of some kind.

>Do I have to just read one byte at a time and assemble my variables
>on-the-fly?

>This seems a bit stupid as Perl is not fussy over reading whatever
>type I want out of any file at any time.

Use a TStream descendant (TMemoryStream, TFileStream etc)  like (untested) ...

type
  THeader3 = string[40];
  THeader4 = array[0..1] of double;
var
  MS : TMemoryStream;
  MyFileData : TMyFileData;
  H1 : integer;
  H2 : array[0..8] of integer;
  H3 : string[40];
  H4 : THeader4;
  PtrMyFileData : PMyFileData;
  // etc
begin
  MS := TMemoryStream.Create;
  with MS Do begin
    LoadFromFile('MyFileOfData.dat');
    Read(H1, SizeOf(integer));
    Read(H2, SizeOf(integer) * H1);
    Read(H3[1], SizeOf(THeader3));
    Read(H4, SizeOf(THeader4);
    // etc
  end;
  PtrMyFileData := AllocMem(SizeOf(TMyFileData));
  with PtrMyFileData^ do begin
    Header1 := H1;
    Header2 := H2;
    Header3 := H3;
    Header4 := H4;
    // etc

  // finally
  MS.Free;
end;

You can also write small functions which read and return the appropriate stuff.
I use that a lot of the time to store and load data from strings, integers,
doubles, records etc.

There is a small help file I wrote on streams, which is available on the FAQ
site for this NG. Can't remember where it is but someone will post the site I'm
sure.

Alan Lloyd
alangll...@aol.com

Re:Delphi doesn't let me read files the way I want


Quote
In article <59b1f153.0304101048.77c15...@posting.google.com>, Mark Smigel wrote:
> What is the de-facto method for handling typed files where the
> record lengths and variables are unpredictable (i.e. dependent on
> the content)?

None. Typed files are defined as a lineair sequence of constant records.

So you'll have to go untyped, don't forget to set the 2nd parameter of reset to 1.

Streams are built on top of these.

Other Threads