[fpc-pascal] accessing files from a function

Rainer Stratmann RainerStratmann at t-online.de
Mon May 16 13:03:18 CEST 2011


Am Monday 16 May 2011 12:28:19 schrieb johnelee1944 at googlemail.com:
> thanks all - btw it wasn't the real code just an example I made up , the
> while loop wasn't needed- mistake!
>
> But the q still remains - having three functions would still give the
> problem I have now- that when doing a file_read (my function flag=1) would
> say 'file not open' because the handles are volatile & local to file reset
> - that was the point of my original email. Seems as if there is no
> solution? John

Put

var f_global : text;

on top of the three functions then it remains the content and you can have 
access from all 3 functions to it.
Alternatively you can make a class or an object with the text variable in it.

type
 myfileobj = object
  file_var : text;
  file_name : string;
  function file_open...
  function file_read...
  function file_close...
 end;

var
 myfile : myfileobj;

myfile.file_open( ...
myfile.file_read( ...
myfile.file_close( ...

> On , Rainer Stratmann <RainerStratmann at t-online.de> wrote:
> > In my eyes it is (very) unlogic.
> >
> >
> >
> > Why don't you make 3 functions?
> >
> > Something like this...
> >
> >
> >
> > var filename : string; // global var for storing the filename
> >
> >
> >
> > function file_reset( name : string ) : boolean;
> >
> > function file_read( var eof : boolean ) : string;
> >
> > function file_close : boolean;
> >
> >
> >
> > Whith your example you don't have to pass the filename for operation 1
> >
> > (flag=1).
> >
> >
> >
> > Rainer
> >
> > Am Monday 16 May 2011 02:28:14 schrieb John Lee:
> > > 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