Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Tutorial on Working with File I/O in VB6

Total Hit ( 3938)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Reading from a file

Click here to copy the following block
Public Function ReadFromFile(FilePath As String) As String
  Dim iFile As Integer
  Dim s As String
  '//Check for File Path
  If Dir(FilePath) = "" Then Exit Function

  On Error GoTo ErrorHandler:

  iFile = FreeFile
  Open FilePath For Input As #iFile
  s = Input(LOF(iFile), #iFile)
  ReadFromFile = s

ErrorHandler:
  If iFile > 0 Then Close #iFile
End Function

Writing in to a file

You can use following function to append any log entry or just writing to a file without appending

Click here to copy the following block
Public Function SaveToFile(Content As String, _
  FilePath As String, Optional Append As Boolean = False) _
  As Boolean

  'If Append = true, then content
  'will be appended to existing file contents
  'else existing file is overwritten

  'Returns: True if Successful, false otherwise

  Dim iFile As Integer

  iFile = FreeFile
  If Append Then
    Open FilePath For Append As #iFile
  Else
    Open FilePath For Output As #iFile
  End If

  Print #iFile, Content
  SaveToFile = True

ErrorHandler:

  Close #iFile
End Function

Reading file content into a string variable ( Reading in Binary mode)

Click here to copy the following block
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

  Dim f As Integer
  Dim s As String
  handle = FreeFile
  Open filename For Binary Access Read As handle
  s = Space(LOF(handle))
  Get #handle, , s
  Close handle
  FileText = s
End Function

Reading from a file line by line

This example uses the EOF function to detect the end of a file. This example assumes that MYFILE is a text file with a few lines of text.

Click here to copy the following block
Dim InputData,hFile
hFile=FreeFile()
Open "C:\MYFILE.txt" For Input As #hFile  ' Open file for input.
Do While Not EOF(hFile)  ' Check for end of file.
  Line Input #hFile, InputData  ' Read line of data.
  Debug.Print InputData  ' Print to the Immediate window.
Loop
Close #hFile  ' Close file.



Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.