Sign in Join

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

Mailing lists / fpc-pascal / 2009-03

Using TFileStream class

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Aurélie de LUCA

Hy everybody,

I'm trying to use the TFileStream class to read a file with a record, and I obtain this message when I execute my application : Access violation. You can find my code in the following :

type
    DmatLine= record
          ID1: string;
          ID2: string;
          C: integer;//classe différence 1 if reactions aren't of same
classe and 0, on the contrary.
          Euclide: double;//Euclidian distance between two reactions.
          Tanimoto: double;//Tanimoto coefficient between two reactions.
          Dice: double;//Dice coefficient between two reactions.
    end;

....

procedure TReadDmat.ReadDmat(input: string; nbCpd : integer);
var
   i, j, k, l, m, sizeOfArray, test: integer;
   DmatFile: TFileStream;
   LineRecord1, LineRecord2: DmatLine;
begin
     if (FileExists(input) )then
     begin
          DmatFile:=TFileStream.Create(input, fmOpenRead);
          try
             DmatFile.Position := 0;
             while (DmatFile.position < DmatFile.size) {NB ^ see above..} do
             begin
                   with LineRecord1 do
                   begin
                        DmatFile.Read(ID1, sizeOf(string));
                        DmatFile.Read(ID2, sizeOf(string));
                        DmatFile.Read(C, sizeOf(integer));
                        DmatFile.Read(Euclide, sizeOf(float));
                        DmatFile.Read(Tanimoto, sizeOf(float));
                        DmatFile.Read(Dice, sizeOf(float));
             end;
             writeln(FloatToStr(DmatFile.size));
             finally
             DmatFile.free;
             writeln('toto');
          end;
     end else begin
              writeln('ERROR: File '+input+' does not exist');
              halt;
     end;
end;

I don't undestand nor what it append and nor how use this information once I could read it.

My file is very big, so, I need use stream and to do some calculations because to do two boucles on my file isn't possible when I use AssignFile and reset.

If you have some tutorials or concils please, it could be help me. I searched on net, on lazarus wiki and documentation and mailing list, but I didn't find what I need.

Thank you for your help,

Cheers,

Aurélie de LUCA.

Howard Page-Clark
to Aurélie de LUCA
Show quoted text 36 lines
Hy everybody, I'm trying to use the TFileStream class to read a file with a record, and I obtain this message when I execute my application : Access violation. You can find my code in the following : type DmatLine= record ID1: string; ID2: string; C: integer;//classe différence 1 if reactions aren't of same classe and 0, on the contrary. Euclide: double;//Euclidian distance between two reactions. Tanimoto: double;//Tanimoto coefficient between two reactions. Dice: double;//Dice coefficient between two reactions. end; .... procedure TReadDmat.ReadDmat(input: string; nbCpd : integer); var i, j, k, l, m, sizeOfArray, test: integer; DmatFile: TFileStream; LineRecord1, LineRecord2: DmatLine; begin if (FileExists(input) )then begin DmatFile:=TFileStream.Create(input, fmOpenRead); try DmatFile.Position := 0; while (DmatFile.position < DmatFile.size) {NB ^ see above..} do begin with LineRecord1 do begin DmatFile.Read(ID1, sizeOf(string)); DmatFile.Read(ID2, sizeOf(string));

(snip)

One problem is your use of long strings in a record structure. Long strings can be any size, and the size can variables are pointers to dynamically allocated storage, so Sizeof(string) will always return 4 on 32bit systems. If DMatLine.ID1 and DmatLine.ID2 are <= 255 characters, you could use shortstrings, declared e. g. as
type
string40 = string[40];

DmatLine= record

ID1: string40; ID2: string40; C: integer;
		...
	   end;

Then the compiler knows exactly how big each record is, and does the TFileStream.

Howard