Date: 02-16-2022
Return to Index
created by gbSnippets
'Works with PowerBASIC FileAttr function and the file handle
'used in the Open statement to determine if a file has already been opened.
'Primary Code:
'Syntax: lResult& = FileAttr([#] filenum&, fattr)
result& = FileAttr(#1,1) '1-Input 2-Output 4-Random 8-Append
' 16-Serial 32-Binary 64-TCP 128-UDP
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg As DWord
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Push", 50,10,100,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
Local FileHandle&
FileHandle& = FreeFile 'ensure FileHandle& is not in use
Open "myfile.txt" For Output As FileHandle& 'remove this to see how program responds
MsgBox GetFileMode(FileHandle&)
Close FileHandle&
End If
End Function
Function GetFileMode(FileHandle&) As String
If FileAttr(FileHandle&,0) Then 'is Open 'FileAttr tests to see if file is Open
Select Case FileAttr(FileHandle&,1) 'FileAttr returns mode of open file
Case 1: Function = "File is open: Input mode"
Case 2: Function = "File is open: Output mode"
Case 4: Function = "File is open: Random mode"
Case 8: Function = "File is open: Append mode"
Case 16: Function = "File is open: Serial mode"
Case 32: Function = "File is open: Binary mode"
Case 64: Function = "File is open: TCP mode"
Case 128: Function = "File is open: UDP mode"
End Select
Else
Function = "File is closed."
End If
End Function
'gbs_00151
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm