Sign in Join

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

Mailing lists / fpc-pascal / 2002-03

files handler in FPC

3 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

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

Łukasz Leśniak

Hello!
My name is Lukasz Lesniak and I am from Poland (Central Europe). I use Pascal very often.

I've declared structure:
(* ------------------------------------------------------------ *)
type Person = record
		name: string[20];
		.
		{something else}
		{about 40 position}
		.
		age : Word;
              end;
     SetOfPerson = ^ElemOfSetOfPerson;
     ElemOfSetOfPerson = record
                           desc: Person;
			   prev: SetOfPerson;
			   next: SetOfPerson;
			 end;
   filePerson = file of Person;
(* ------------------------------------------------------------ *)

First I wrote my program in Turbo Pascal. Now I want to convert source to FPC and the problem is:

(* ------------------------------------------------------------ *)
procedure ReadDataFromFile(var s: SetOfperson)
var F: filePerson;
    p: SetOfPerson;
.
.
Assign(F, path_to_F);
{$i-}
Reset(F);
{$i+}
if IOResult  0 then Halt(1);
while not Eof(F) do
 begin
  new(p);
  Read(F, p^.desc);
  p^.next:=s;
  s:=p;
end;
.
.
(* ------------------------------------------------------------ *)

and trying the same in FPC

(* ------------------------------------------------------------ *)
procedure ReadDataFromFile(var s: SetOfperson)
var i, F: LongInt;
    p: SetOfPerson;
.
.
 i:=0;
 F:=FileOpen(path_to_F, fmOpenRead);
 if F = -1 then Halt(1);
 new(p);
 FileSeek(F, i, fsFromBeginning);
 FileRead(F, p^.desc, SizeOf(p^.desc));
 (* XXX *)
 p^.next:=s;
 s:=p;
 i:= i + 1;
 end;
(* ------------------------------------------------------------ *)

XXX - question is: how to read file with FileRead function from beginning to end, beacause this code reads only one person! I am using FileSeek. It is good?

_____________________________________
/* created by luke BFTH */

Adam Naumowicz
to Łukasz Leśniak

Hello,

Hello! My name is Lukasz Lesniak and I am from Poland (Central Europe).

Although I'm in Japan now, I remember where Poland is quite well :-))

Show quoted text 71 lines
I use Pascal very often. I've declared structure: (* ------------------------------------------------------------ *) type Person = record name: string[20]; . {something else} {about 40 position} . age : Word; end; SetOfPerson = ^ElemOfSetOfPerson; ElemOfSetOfPerson = record desc: Person; prev: SetOfPerson; next: SetOfPerson; end; filePerson = file of Person; (* ------------------------------------------------------------ *) First I wrote my program in Turbo Pascal. Now I want to convert source to FPC and the problem is: (* ------------------------------------------------------------ *) procedure ReadDataFromFile(var s: SetOfperson) var F: filePerson; p: SetOfPerson; . . Assign(F, path_to_F); {$i-} Reset(F); {$i+} if IOResult 0 then Halt(1); while not Eof(F) do begin new(p); Read(F, p^.desc); p^.next:=s; s:=p; end; . . (* ------------------------------------------------------------ *) and trying the same in FPC (* ------------------------------------------------------------ *) procedure ReadDataFromFile(var s: SetOfperson) var i, F: LongInt; p: SetOfPerson; . . i:=0; F:=FileOpen(path_to_F, fmOpenRead); if F = -1 then Halt(1); new(p); FileSeek(F, i, fsFromBeginning); FileRead(F, p^.desc, SizeOf(p^.desc)); (* XXX *) p^.next:=s; s:=p; i:= i + 1; end; (* ------------------------------------------------------------ *) XXX - question is: how to read file with FileRead function from beginning to end, beacause this code reads only one person! I am using FileSeek. It is good?

I can't see no loop here ! How do you think this is supposed to read all the list ?
BTW - the BP snippet you included is a little old style but I can't see there anything the FPC compiler should complain at. Regards,
Adam Naumowicz

--------------------------------------
WWW: http://math.uwb.edu.pl/~adamn/
--------------------------------------

Adam Naumowicz
to Łukasz Leśniak: Hello! My name is Lukasz Lesniak and I am from Poland (Central Europe). I use Pascal very…
Show quoted text 16 lines
procedure ReadDataFromFile(var s: SetOfperson) var i, F: LongInt; p: SetOfPerson; . . i:=0; F:=FileOpen(path_to_F, fmOpenRead); if F = -1 then Halt(1); new(p); FileSeek(F, i, fsFromBeginning); FileRead(F, p^.desc, SizeOf(p^.desc)); (* XXX *) p^.next:=s; s:=p; i:= i + 1; end;

Even if you have a loop there is still one more thing: if you want to read the file sequentially there is no need to set the file position with FileSeek since FileRead changes the current position. But anyway, if you read a raw data type (as in this case) you should increment the position by SizeOf buffer, not by 1.

Best regards,
Adam Naumowicz

--------------------------------------
WWW: http://math.uwb.edu.pl/~adamn/
--------------------------------------