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 array with a single Line Input# statement.
'Primary Code:
Dim count&
Open "myfile.txt" For Input As #1
FileScan #1, RECORDS TO count& 'get count of lines of text in the file
Dim MyArray(count&-1) As String 'Dim 0 to count-1
Line Input #1, MyArray() TO count& 'Get entire array in one gulp, count& gives # lines read
Close #1
'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
Dim count&
Open "myfile.txt" For Input As #1
FileScan #1, RECORDS TO count& 'get count of lines of text in the file
Dim MyArray(count&-1) As String 'Dim 0 to count-1
Line Input #1, MyArray() TO count& 'Get entire array in one gulp, count& gives # lines read
Close #1
MsgBox Join$(MyArray(), $crlf)
End If
End Function
'gbs_00159
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm