Date: 02-16-2022
Return to Index
created by gbSnippets
'While text files can be read one line at a time, the entire
'content can be passed to a string with a single GET statement.
'Primary Code:
Dim filedata$
Open "myfile.txt" For Binary As #1
Get$ #1, Lof(1), filedata$ 'filedata$ will contain $crlf's from the file
Close #1 'LOF() returns number of bytes in an Open file
'Note: The string can be loaded into an array using the ParseCount/Parse
'functions as given in these next two statements.
Dim MyArray(ParseCount(filedata$))
Parse filedata$, MyArray(), $crlf
'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
CreateTestFile 'create sample data for this test
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
Sub CreateTestFile
Open "myfile.txt" For Output As #1
Print #1, "One" : Print #1, "Two" : Print #1, "Three"
Close #1
End Sub
CallBack Function DlgProc() As Long
If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
'using LOF() to capture entire file in a string
Dim filedata$
Open "myfile.txt" For Binary As #1
Get$ #1, Lof(1), filedata$ 'filedata$ will contain $crlf's from the file
Close #1
MsgBox filedata$ 'data read using GET$
End If
End Function
'gbs_00160
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm