What does # do in "Open For" and "Put"?

Viewed 54

I have often seen such code:

Dim fnum As Integer
fnum = FreeFile()
Open "myFile.txt" For Binary As #fnum
Put #fnum, 1, CA
Close fnum

It seems a bit strange to me. In "Open For", the author uses "#fnum", and in "Put", too, but not in "Close".

Therefore I would like to ask what it actually used for. Does this originate from version of Visual Basic earlier than 6?

1 Answers

They are file handles: https://stackoverflow.com/a/22847396/2467152

Those VB6 commands (FreeFile, FileOpen, FileClose, LOF, etc.) were present at least as far back as QBasic. I expect that the file number was originally an MS-DOS file handle.

Back in QBasic (if memory serves), the FileOpen command opened the file and reserved the file handle. The FileClose command closed the file and freed the handle.

FreeFile was just a convenience method to get an unused file handle: if you knew that you didn't have (for example) file #1 open, then you could just call OPEN "C:\DOS\RUN" FOR INPUT AS #1 and not bother calling FreeFile. You would still close it with CLOSE #1

Related