How to read & save file in VB6

Private Sub Form_Load()
Dim lines() As String
lines = Split(LoadFile(“C://db.txt”), vbCrLf)
For i = 0 To UBound(lines)
MsgBox lines(i)
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
End Sub

Public Function LoadFile(dFile As String) As String

Dim ff As Integer

On Error Resume Next

ff = FreeFile
Open dFile For Binary As #ff
LoadFile = Space(LOF(ff))
Get #ff, , LoadFile
Close #ff

End Function


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s