Recent

Author Topic: [SOLVED] Trying to understand differences between assign/reset and FileOpen  (Read 480 times)

tfurnivall

  • New Member
  • *
  • Posts: 49
In general, when I totally don't understand something, I try and create a Lab environment where I can beat up on the problem until I begin to get some understanding. In my current project (The Big Project) I am separating all file interactions in one unit, building a table of all the files I may or may not be using, and then referencing them via the file handle.

To do this, I need to get the file handle - regardless of how I open it. FileOpen gives me the handle directly, but I can also get at the handle by using GetFileHandle (I think). So, I created the attached lab to explore TestFiles.zip).

I'm using only text files at this spoint - and Pascal source is a readily available text file!

When I select a file and open it with FileOpen, it returns a handle. When I select a file and open it with Assign/Reset/GetFileHandle it also returns a handle. In order to be able to test the 'open' process, I use the handle to close the file, if it is non-zero:

Code: Pascal  [Select][+][-]
  1.     if testfilehandle > 0 then
  2.        closefile(testfile);
  3.  

This works fine if I open with Assign/Reset/GetHandle and then try and close with FileClose or simply Close.

However, the same code, using the handle from FileOpen fails to close the file because it is not Open (even though no error seems to be reported).

The lab is (I think) fairly self-evident: Select a file (maybe one of the files from the unzipped project?), and then try various Open/Close sequences. The Top line of buttons use the handle-based routines from SysUtils, the bottom buttons use the traditional Pascal Reset routines.

I know this is obvious and staring me in the face - it's just that no matter how hard I stare back, I can't see it!

Grateful, as always, for any pointers and advice!

Tony

« Last Edit: May 28, 2025, 02:53:22 pm by tfurnivall »

PeterBB

  • Jr. Member
  • **
  • Posts: 75
Hi,

I think in the routine
TfrmTestFiles.cmdFileCloseClick(Sender: TObject);

where you have
Code: Pascal  [Select][+][-]
  1. closefile(testfile);

it should be
Code: Pascal  [Select][+][-]
  1. fileclose(testfilehandle);

Cheers,
Peter

gues1

  • Jr. Member
  • **
  • Posts: 95
........... I use the handle to close the file, if it is non-zero:
Code: Pascal  [Select][+][-]
  1.     if testfilehandle > 0 then
  2.        closefile(testfile);
  3.  

testfilehandle is declared as integer. Why do you think that it cannot have a value less than zero ? In the test you only check for positive value.
And a file handle can have another primary value (among others) that is INVALID_FILE_HANDLE that is equal to "-1" and of course indicate that the filehandle is invalid and should not be used.

Bye

tfurnivall

  • New Member
  • *
  • Posts: 49
Thanks to PeterBB and gues1 for their responses.

PeterBB - Yes I fatfingered the FileClose into closefile (Compiler didn't hiccup but you are right, it is not what I meant to write). I changed it back - and made certain that I was referencing the handle rather than the file, Bingo! I'm now ready to try adding the Read options (and eventually the Write options).

gues1 - your question is also relevant. I note, from the documentation (rtl manual, sysutils section, FileOpen) that a file handle is set to -1 if an error occurs. Hence my test for a valid file handle being > 0. I suppose, in theory, that it could also be 0, but my intuition tells me that the handle is actually a pointer to a table entry somewhere, and negative or zero values don't make much sense in that context. When I think of a handle I regard it as a pointer to whatever the operating system needs to handle a particular FileOpen. This includes such things as buffers, access rights, disc locations, etc. Some of this is unique to a particular FileOpen, and some of it (the physical pointers to disc space) is shared.

Windows and Unix have very rudimentary file operations - 'mainframe' operating systems typically provide a much richer set of capabilities. The basis for my Big Project is that at any moment I need to be able to read not only from text files, but also from files organized as records (typed files in Lazarus Lingo), or even from some part of a binary file that just happens (conveniently) to be formatted as if it were a text file. For this reason, I need to get a handle from every file I open, and then do all my I/O via that handle.

Probably more info than you wanted - but the reason I test only FileHandle > 0 is that if FileHandle < 0 it probably isn't valid, so shouldn' t be used.

Thanks for you interest and comments!

Tony

 

TinyPortal © 2005-2018