Recent

Author Topic: [SOLVED]File Read using Block Read - Segmentation fault  (Read 4721 times)

wujiyan

  • New Member
  • *
  • Posts: 16
I have a Class procedure named LoadData which read records to dynamic array(1 based) of records written to a file. Initially array size is set to 2 and each time a record is read  , array size is incremented using the SetLength procedure.

When I am running this code, after reading the records, following error is returned .
'Segmentation fault (core dumped)'

I am wondering why is this happening. Please help ..

Code: [Select]
type
   TPerson = record
      name : String[20];
      age : Integer;
      gender : String[10];
      phNum : String[15];
   end;

type
   TPersonDetails = class
      names : array of Tperson;
      count : Integer;
      procedure SaveData;
      procedure LoadData;
      procedure Display;
   end;

procedure TPersonDetails.SaveData;
   var
      f : file of TPerson;
      i : Integer;
   begin
      {$i+}
      AssignFile(f,'/home/user1/pers.bat');
      ReWrite(f);
      {$i+}
      try
         for i := 1 to count - 1 do begin
            BlockWrite(f,names[i],SizeOf(names[i]));
         end;
      finally
         WriteLn('File Write Successfull');
         CloseFile(f);
      end;
   end;

procedure TPersonDetails.LoadData;
   var
      i : Integer;
      f : file of TPerson;
   begin
      i := 2;
      SetLength(names,i);
      count := 1;
      {$i-}
      AssignFile(f,'/home/user1/pers.bat');
      Reset(f);
      {$i+}
      try
         while not EOF(f) do begin
            BlockRead(f,names[count],SizeOf(names[count]));
            SetLength(names,i+1);
            count := count + 1;
         end;
      finally
         CloseFile(f);
         WriteLn('Read successfully');
      end;
   end;   

After calling TPersonDetails.LoadData

Read successfully
Segmentation fault (core dumped)

« Last Edit: June 02, 2015, 01:30:53 am by wujiyan »

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: File Read using Block Read - Segmentation fault
« Reply #1 on: May 31, 2015, 03:28:02 pm »
Index of dynamic arrays always starts with 0 (zero). You need to change your program so it will take this in the mind.

wujiyan

  • New Member
  • *
  • Posts: 16
Re: File Read using Block Read - Segmentation fault
« Reply #2 on: May 31, 2015, 03:51:00 pm »
Index of dynamic arrays always starts with 0 (zero). You need to change your program so it will take this in the mind.

Thanks, but I am setting the array length to 2, and using the Ist Index and in the while loop I am incrementing the length.

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: File Read using Block Read - Segmentation fault
« Reply #3 on: May 31, 2015, 03:54:40 pm »
Index of dynamic arrays always starts with 0 (zero). You need to change your program so it will take this in the mind.

Thanks, but I am setting the array length to 2, and using the Ist Index and in the while loop I am incrementing the length.

See FPC documentation about dynamic arrays : http://www.freepascal.org/docs-html/ref/refsu18.html#x42-480003.3.1

You need to set its length to 3, if you want to start with index 1.
With length 2, minimum index is 0 and maximum 1.

wujiyan

  • New Member
  • *
  • Posts: 16
Re: File Read using Block Read - Segmentation fault
« Reply #4 on: May 31, 2015, 04:55:12 pm »

Quote

See FPC documentation about dynamic arrays : http://www.freepascal.org/docs-html/ref/refsu18.html#x42-480003.3.1

You need to set its length to 3, if you want to start with index 1.
With length 2, minimum index is 0 and maximum 1.

Thanks for the link. I will go through the docs..

Laksen

  • Hero Member
  • *****
  • Posts: 794
    • J-Software
Re: File Read using Block Read - Segmentation fault
« Reply #5 on: May 31, 2015, 08:16:04 pm »
You need Rewrite(f,1); and Reset(f,1); when using blockread with a byte sized argument. You should also use file instead of file of X in that case

wujiyan

  • New Member
  • *
  • Posts: 16
Re: File Read using Block Read - Segmentation fault
« Reply #6 on: May 31, 2015, 08:44:49 pm »
You need Rewrite(f,1); and Reset(f,1); when using blockread with a byte sized argument. You should also use file instead of file of X in that case

Thanks a mil, Laksen. I followed your instructions and made the changes and my flleload procedure is working like charm ..

modified procedure

Code: [Select]
procedure TPersonDetails.LoadData;
   var
      i : Integer;
      f : file;
   begin
      i := 2;
      FillChar(names,SizeOf(names),#0);
      SetLength(names,i);
      count := 1;
      {$i-}
      AssignFile(f,'/home/bladerunner/cpgms/pascalFiles/nnn.bat');
      Reset(f,1);
      {$i+}
      try
         while not EOF(f) do begin
            BlockRead(f,names[count],SizeOf(names[count]));
            SetLength(names,i+1);
            i := i + 1;
            count := count + 1;
         end;
      finally
         SetLength(names,count- 1);
         count := count - 1;
         CloseFile(f);
      end;
   end; 

eny

  • Hero Member
  • *****
  • Posts: 1646
Re: File Read using Block Read - Segmentation fault
« Reply #7 on: May 31, 2015, 11:02:31 pm »
You need Rewrite(f,1); and Reset(f,1); when using blockread with a byte sized argument. You should also use file instead of file of X in that case
Or use file of TPerson and read/write (and not blockread/blockwrite).
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

wujiyan

  • New Member
  • *
  • Posts: 16
Re: File Read using Block Read - Segmentation fault
« Reply #8 on: June 01, 2015, 02:07:58 am »
Quote
Or use file of TPerson and read/write (and not blockread/blockwrite).

Thanks Eny, I will try this as well ..       
 


 

TinyPortal © 2005-2018