Sign in Join

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

Mailing lists / fpc-pascal / 2011-05

accessing files from a function

9 messages · 7 people ·

Reply by email Subscribe to this list Your subscription

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

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;

Reply by email

Ralf A. Quint
to John Lee
John Lee wrote 12 lines
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

Only way would be if you pass the file as a parameter. But then you can put the file open sequence at the same place as you are calling the function with the "0" parameter and close it instead of calling with parameter "2".

Beside that, the function wouldn't work anyway, as
- for call with the "0" parameter, you don't get a line back, likewise negating any "saving" as you intend
- the while loop is nonsense, you would always get the last line of the file...

Ralf

Reply by email

London Disney
to John Lee: I'd like to put the assign and reset of a text file into a function, and then use the…

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

implementation
var
f:text
....
.....

then have a separate function to close the file.

John Lee wrote 37 lines
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 - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Rainer Stratmann
to John Lee: I'd like to put the assign and reset of a text file into a function, and then use the…

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

John Lee wrote 40 lines
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;

Reply by email

Unknown author
to Rainer Stratmann

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

Rainer Stratmann 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
Show quoted text 5 lines
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
Show quoted text 84 lines
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;
Show quoted text 5 lines
_______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Unknown author
to Unknown author
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

Of course there is. As was stated earlier you need to make the filehandle a global not a local variable. You could also handle it differently and use a TStringList instead.

var
 s: TStrings;
s :=
TStringList.Create;
s.LoadFromFile('MyFile.txt');
//do something with
line 12
writeln(s[12]);
s.Free

Regards, Darius

Reply by email

Rainer Stratmann
to Unknown author: thanks all - btw it wasn't the real code just an example I made up , the while loop…

Am Monday 16 May 2011 12:28:19 schrieb

Show quoted text 8 lines
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( ...

Show quoted text 123 lines
On , Rainer Stratmann 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 - > > http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Doug Chamberlin
to John Lee: I'd like to put the assign and reset of a text file into a function, and then use the…
John Lee 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 sounds like an attempted solution to a problem and you are having trouble with your solution. Since several people have expressed reservations about your attempted solution, perhaps if you shared the *actual problem* you are trying to solve we could suggest a better approach to a solution?

Reply by email

Anton Shepelev
to Rainer Stratmann: Am Monday 16 May 2011 12:28:19 schrieb Put var f_global : text; on top of the three…

Rainer Stratmann:

Alternatively you can make a class or an object with the text variable in it.

But if you need to work with only one file, you can get rid of a global variable in the main module by just extracting the relevant functions and the file handle into a separate unit:

---------- myfile.pas -------------------------------------
Unit MyFile;
Interface
   Procedure OpenFile(fullname: ShortString);
   Function  ReadNextLine(): ShortString;
   Procedure CloseFile();
   Function  IsFileRead(): Boolean;
Implementation
   var
      TheFile: TEXT;
   Procedure OpenFile(fullname: ShortString);
   begin
      Assign(TheFile, fullname);
      Reset(TheFile);
   end;
   Function ReadNextLine(): ShortString;
   var
      line: ShortString;
   begin
      ReadLn(TheFile, line);
      ReadNextLine := line;
   end;
   Procedure CloseFile();
   begin
      Close(TheFile);
   end;
   Function IsFileRead(): Boolean;
   begin
      IsFileRead := Eof(TheFile);
   end;
end.
----------- filetest.pas ---------------------------------
Program ftest;
uses MyFile;
begin
   OpenFile('test.txt');
   While not IsFileRead() do
      WriteLn(ReadNextLine());
   CloseFile();
end.
----------------------------------------------------------

Anton

Reply by email