Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Visual Basic Programming Code Examples

Visual Basic > Files Directories Drives Code Examples

Reading text files (into arrays or string variables)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
Reading text files (into arrays or string variables) Listed below are two routines for opening and reading text files. The first reads the file into a string array and the second function simply returns the contents of the file as a single continuous string. Note, a demonstration routine can be found at the bottom of this post. Option Explicit 'Purpose : Reads a file into a string array. 'Inputs : asLines() A string array (see Outputs) ' sFileName The path and file name of the file to open and read 'Outputs : Returns an empty string on success, else returns the error decription ' asLines(1 to NumLines) String array containing the file 'Notes : Usually used for text files, but will load any file type. Function FileLoadToArray(ByRef asLines() As String, ByVal sFileName As String) As String Dim iFileNum As Long, lFileLen As Long Dim sBuffer As String 'Initialise Variables On Error GoTo ErrFailed 'Open File iFileNum = FreeFile Open sFileName For Binary Access Read As #iFileNum 'Get the size of the file lFileLen = LOF(iFileNum) If lFileLen Then 'Create output buffer sBuffer = String(lFileLen, " ") 'Read contents of file Get iFileNum, 1, sBuffer 'Split the file contents asLines = Split(sBuffer, vbNewLine) End If Close #iFileNum 'Return success FileLoadToArray = "" Exit Function ErrFailed: Debug.Assert False Debug.Print Err.Description FileLoadToArray = Err.Description 'Close file If iFileNum Then Close #iFileNum End If End Function 'Purpose : Returns the contents of a file as a single continuous string 'Inputs : sFileName The path and file name of the file to open and read 'Outputs : The contents of the specified file 'Notes : Usually used for text files, but will load any file type. 'Revisions : Function FileLoad(ByVal sFileName As String) As String Dim iFileNum As Integer, lFileLen As Long On Error GoTo ErrFinish 'Open File iFileNum = FreeFile 'Read file Open sFileName For Binary Access Read As #iFileNum lFileLen = LOF(iFileNum) 'Create output buffer FileLoad = String(lFileLen, " ") 'Read contents of file Get iFileNum, 1, FileLoad ErrFinish: Close #iFileNum On Error GoTo 0 End Function 'Demonstration routine Sub Test() Dim asFileContents() As String 'Read the file into a string array Dim sFileContents As String 'Read the contents into a string Dim lThisLine As Long, lNumLines As Long Dim sFileName As String 'Enter the filename of a text file here: sFileName = "C:\boot.ini" Debug.Print "-----------------------------------------" Debug.Print "Loading " & sFileName & " using an array:" '---Method 1. Load a file into an array Call FileLoadToArray(asFileContents, sFileName) For lThisLine = 1 To Ubound(asFileContents) Debug.Print asFileContents(lThisLine) Next Debug.Print "-----------------------------------------" Debug.Print "Loading " & sFileName & " using a string:" '---Method 2. Load a file into a string sFileName = FileLoad(sFileName) Debug.Print sFileName End Sub