Recent

Author Topic: Working with an array of type TextFile  (Read 471 times)

Wilko500

  • Full Member
  • ***
  • Posts: 180
Working with an array of type TextFile
« on: March 01, 2025, 12:39:52 pm »
MacOS LazFPC 40RC2/323

The program re-write I am working on requires that I log inverter data every 1 minute to text files. There are 16 log files.  The inverter data that is being read will be stored in a record or array.  It seems that from an efficiency point of view a good approach is to open all the files at start of program and leave them open thus avoiding resource expensive open/close operations.

While considering how to program this other than a list of statements for open, update and then close it occurred to me that I am dealing with 16 identical processes so why not place them in arrays.  The following code snippet shows what I have tried.  While I did not expect it to work it does but I don't understand why. The line that puzzles me is
Code: Pascal  [Select][+][-]
  1.   arFile: Array[1..3] Of TextFile;  
because I don't initialise the contents of arFile thus I would have expected that each element is null but I do end up with multiple files being created as desired.

Have I been lucky that my code snippet works?  Is there a better/safer way to code this task?

Code: Pascal  [Select][+][-]
  1. Var
  2.   i:   Int32;
  3.   num: Single;
  4.   arFile: Array[1..3] Of TextFile;
  5.   arFileName: Array[1..3] Of String;
  6.  
  7. Begin
  8.    for i:= 1 to 3 do
  9.        begin
  10.        arFileName[i]:= g.DebugLogFilePath + '/File' + IntToStr(i) + '.cht';
  11.        assignFile(arFile[i],arFileName[i]);
  12.        ReWrite(arFile[i]);
  13.        end;
  14.  
  15. For i:= 1 to 3 do
  16.     Begin
  17.     writeln(arFile[i], 'Hello textfile!');
  18.     writeln(arFile[i], 'Hello textfile Line two');
  19.     CloseFile(arFile[i]);
  20.     end;  
MacBook Pro mid 2015 with OS Monterey 12.7.6
FPC 3.2.3 Lazarus 3.7
FPC 3.2.2 Lazarus 3.4

dseligo

  • Hero Member
  • *****
  • Posts: 1674
Re: Working with an array of type TextFile
« Reply #1 on: March 01, 2025, 01:00:52 pm »
because I don't initialise the contents of arFile thus I would have expected that each element is null but I do end up with multiple files being created as desired.

arFile[ i ] is being initialized with assignFile (which is same as System.Assign) - in line 11 of your example.

Wilko500

  • Full Member
  • ***
  • Posts: 180
Re: Working with an array of type TextFile
« Reply #2 on: March 01, 2025, 02:44:59 pm »
Thanks for that clarification.  I looked up System.Assign which I didn't know was a thing :)  Makes sense now. While I have used AssignFile in its simplest context many times I never really considered what it was doing. I see now that my arFile element is no different to a single Var. 
Code: Pascal  [Select][+][-]
  1. Assign assigns a name to F, which can be any file type. This call doesnt open the file, it just assigns a name to a file variable, and marks the file as closed
  2. Note that the filename (including path) can be only 255 characters long.
MacBook Pro mid 2015 with OS Monterey 12.7.6
FPC 3.2.3 Lazarus 3.7
FPC 3.2.2 Lazarus 3.4

dseligo

  • Hero Member
  • *****
  • Posts: 1674
Re: Working with an array of type TextFile
« Reply #3 on: March 01, 2025, 04:30:33 pm »
I see now that my arFile element is no different to a single Var.

Exactly.

 

TinyPortal © 2005-2018