You can read from (or write to) a text file using the Open statement, Close statement and various other statements for actually reading/writing.
The Open statement has clauses for what type of access you want to the file, so you can specify if you want to read it as text, write to it as binary, and so on. You can also set locking options, to stop other programs from opening the file. For more details, go to the Index of VB help and see "Open statement".
For all of these file access functions you need to specify a file number, which most people use 1 for. You should always use a number which is not already in use (as using the same will cause an error), luckily the next free number can be found using the FreeFile function as in the example below.
An example of reading a file:
(note: an alternative to Input # is Line Input # , which reads whole lines).VB Code:
Dim sFileText as String Dim iFileNo as Integer iFileNo = FreeFile 'open the file for reading Open "C:\Test.txt" For Input As #iFileNo 'change this filename to an existing file! (or run the example below first) 'read the file until we reach the end Do While Not EOF(iFileNo) Input #iFileNo, sFileText 'show the text (you will probably want to replace this line as appropriate to your program!) MsgBox sFileText Loop 'close the file (if you dont do this, you wont be able to open it again!) Close #iFileNo
An example of writing a file:
(note: an alternative to Print # is Write # , which adds commas between items, and also puts the " character around strings).VB Code:
Dim sFileText as String Dim iFileNo as Integer iFileNo = FreeFile 'open the file for writing Open "C:\Test.txt" For Output As #iFileNo 'please note, if this file already exists it will be overwritten! 'write some example text to the file Print #iFileNo, "first line of text" Print #iFileNo, " second line of text" Print #iFileNo, "" 'blank line Print #iFileNo, "some more text!" 'close the file (if you dont do this, you wont be able to open it again!) Close #iFileNo
You can read files in a much faster way too, by simply loading the entire file at once. To do this you need to use binary mode, like so:
VB Code:
Function FileText(ByVal filename As String) As String Dim handle As Integer ' ensure that the file exists If Len(Dir$(filename)) = 0 Then Err.Raise 53 ' File not found End If ' open in binary mode handle = FreeFile Open filename$ For Binary As #handle ' read the string and close the file FileText = Space$(LOF(handle)) Get #handle, , FileText Close #handle End Function
Reading/writing files can also be acheived using FSO (FileSystemObject) as shown below. Please note that using FSO requires an extra reference (under "Project"->"References"), and therefore extra files to be installed with your application.
VB Code:
Option Explicit 'Set a reference to "Microsoft Scripting Runtime" Private Sub Command1_Click() 'declare and initiate required objects Dim fs As FileSystemObject Dim ts As TextStream Set fs = New FileSystemObject 'To write Set ts = fs.OpenTextFile("C:\mytestfile.txt", ForWriting, True) ts.WriteLine "I Love" ts.WriteLine "VB Forums" ts.Close 'To Read If fs.FileExists("C:\mytestfile.txt") Then Set ts = fs.OpenTextFile("C:\mytestfile.txt") Do While Not ts.AtEndOfStream MsgBox ts.ReadLine Loop ts.Close End If 'clear memory used by FSO objects Set ts = Nothing Set fs = Nothing End Sub


Reply With Quote