[fpc-pascal] accessing files from a function

London Disney disney.london at googlemail.com
Mon May 16 08:35:26 CEST 2011

well why not make it a global variable in that unit's implementation?


then have a separate function to close the file.

On 16 May 2011, at 01:28, John Lee <johnelee0 at gmail.com> wrote:

> I'd like to put the assign and reset of a text file into a function, and then use the function to return a line from the file, without needing to close the file then re assign and reset  every time (for performance reasons) - see below for my attempt. 
> This doesn't work because I guess the file handles etc are local to the function are volatile, so cannot be accessed in the main program  Is there a simple way to fix this ie to make this function work. Of course one can const in a function to make normal variables non volatile...but files etc?
> TIA for any ideas...John   
> main program...
> get_line('tmp.tmp,0), to assign and reset;
> {get lines}
> str1:=get_line('tmp.tmp,1);
> str2:=get_line('tmp.tmp,1)
> ....
> {close)
> get_line('tmp.tmp,2) 
> assume function is defined as 
> function get_line( fname;string;flag:longint):string;
> var f:text
> begin
> if flag=0 then
>  begin
>   assign(f,fname);
>   reset(f);
>  end;
> if flag=1 then 
> while not eof(f) do
>  begin
>   readln(f);
>  end;
>  if flag=2 then close(f);
>  get_line;=f;
> end;
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal

More information about the fpc-pascal mailing list