[fpc-pascal]blockread in a procedure

Michael.VanCanneyt at Wisa.be Michael.VanCanneyt at Wisa.be
Sun Feb 29 12:46:34 CET 2004


On Sun, 29 Feb 2004, David Emerson wrote:

> Hi there,
> 
> I'm having trouble using blockread. It was working for... oh, a year... and all of a sudden it's not working. I'm getting a runtime error 87 in win32/i386 (fpc 1.0.10) and an error 217 in linux/i386 (fpc 1.0.6). Since I always compile with -gl it tells me it's failing right at blockread. Here's the code...
> 

I tested some things with the current compiler. The error occurs there as
well. It's a stack problem. If you move the 'buf' definition 
out of the procedure, I.e. define it as a global var, it works fine.

Assuming you don't want to use a global variable (bad programming practice), 
you can also try the following, which works fine as well: 
(ansistrings are allocated on the heap, bypassing the stack);

Michael.

program testb;

const
  test_file_name = 'test.txt';
  the_source : ansistring = '';

const
  bufsize = 2048;

procedure read_source_file;


  var
    source_file : file;
    count_read : longint;   // longint required by blockread
    Buf : AnsiString;
    
  begin
    SetLength(Buf,BufSize);
    write ('Reading source file.....');
    assign (source_file, test_file_name);
    reset (source_file);
    the_source := '';
    repeat
      blockread (source_file, buf[1], bufsize, count_read);
      the_source := the_source + copy (buf, 1, count_read);
      until count_read = 0;
    close (source_file);
    the_source := the_source + chr(255);
    writeln ('Source file successfully read');
  end;

begin
  read_source_file;
end.




More information about the fpc-pascal mailing list