File Processing

 File processing involves three steps: 

 ======================

Opening a File

The Open command is used to open files.

A sequential file is little more than a file that consists of ASCII characters.

The
Open statement has the following syntax:

Open pathname For [Input Output Append] As [#]filenumber

 The following are the components of the syntax:  

Open "C:\VB\VB.TXT" For Input As #1

If you do not specify the path of the file, Visual Basic will assume that the file you are opening is in the current directory, usually the directory from which your application is executing. 

Open "VB.TXT" For Input As #1

You can also use App.Path to specify the directory from which your application is executing. 

Open App.Path & "\VB.TXT" For Input As #1  *

[See the note on App.Path]

You can open more than one file at a time. However, you must use different file numbers for each open file.

======================

Determining the Size of the File

When reading a file, you must know when to stop. Attempting to read past the end of a file will generate an error. Reading less than an entire file may also prove troublesome, depending on your program's needs. 

Visual Basic has two functions that you can use to determine how much to read and when to stop reading: 

---------------------

The syntax for LOF is as follows: 

FileSize = LOF(filenumber) 

---------------------

To determine when you have reached the end of a file, use the EOF function, which has the following format:

EOF(filenumber) 

where filenumber is the file number of the open file. The EOF function returns True when the end of the file is reached.

======================  

Reading a File 

Reading a file means extracting the characters that are in the file into a memory buffer. To a read a file, you must

Visual Basic 6 provides various functions and statements to read a sequential file:

---------------------

Input Function 

The Input function has the following syntax: 

Input (number, [#]filenumber) 

The following are the components of the syntax:

The Input function is used to read any number of bytes from a file into a Visual Basic variable. For instance, the following example uses the Input function to read 10 characters into the string variable LastName: 

LastName = Input(10, #1)

---------------------

Input # Statement 

The Input # statement provides an easy way to read a list of numbers or strings from a file and assign them to variables. The syntax for the Input # statement is 

Input #filenumber, varlist 

The following are components of the syntax: 

Assume that a file contains the following:

Mark, Wednesday 

Using the Input # statement, Mark and Wednesday could be read into local variables Name and DayOfWeek. 

Input #1, Name, DayOfWeek 

Note that the fields in data file are separated by commas. 

---------------------

Line Input # Statement 

The Line Input # statement reads a single line of data at a time.  

The statement will read all the characters until it encounters a carriage return-linefeed sequence and returns all the data up to the carriage return.  

The syntax for Line Input # is 

Line Input #filenumber, varname

The following are the components of the syntax: 

The following example reads an entire line from the file. 

Line Input #1, OneLine

======================  

Writing to a File 

The command to write to a sequential file is the Print # command, which has the following syntax: 

Print #filenumber, [outputlist] 

The following are the components of the syntax: 

The following example uses the Print # statement to write a variable to a file: 

Dim strCount as String
StrCount = "one two three four five"
Print #1, strCount

======================

Closing a File 

The Close statement has the following syntax: 

Close [filenumberlist]

The following code example closes an open file with the file number 1: 

Close #1

======================  
Examples:
======================  
Private Sub cmdInput_Click( )
    Dim Name As String
    Dim exam1 As Integer, exam2 As Integer, exam3 As Integer 
 

   '--------------------------------------------------------------
   
' Read and print contents of sequential file
    '--------------------------------------------------------------
    Picture1.Cls
    Open App.Path & "\grades.txt" For Input As #1

    While Not EOF(1)
        Input #1, Name, exam1, exam2, exam3
        Picture1.Print Name & " scored a " & exam2 & " on the 2nd exam."
    Wend

    Close #1    

   '--------------------------------------------------------------
    ' Write a record to a sequential file
    '--------------------------------------------------------------
    Open App.Path & "\grades.txt" For Append As #1
    Print #1, "Bullwinkle Moose, 88, 92, 77"
    Close #1   

    '--------------------------------------------------------------
    ' Read and print contents of sequential file
    '--------------------------------------------------------------
    Picture2.Cls
    Open App.Path & "\grades.txt" For Input As #1

    While Not EOF(1)
        Input #1, Name, exam1, exam2, exam3
        Picture2.Print Name & " scored a " & exam2 & " on the second exam."
    Wend

    Close #1

End Sub

======================

'--------------------------------------------------------------
' Read the contents of a sequential file
' by reading the entire file at once
'--------------------------------------------------------------
Private Sub readFile(ByRef Read_Buffer As String)
    Dim File_Length As Integer

    Open App.Path & "\namingOfParts.txt" For Input As #1
    File_Length = LOF(1)
    Read_Buffer = Input(File_Length, #1)
    Close #1

End Sub

 

 

===============================================================================

* To insure that App.Path works correctly, use the code below to insure that it ends with a \.

Private Function filePath( ) As Sting
      If lastChar(App.Path) = "\" Then
            filePath = App.Path
      Else
            filePath = App.Path & "\"
      End If
End Function

Private Function lastChar(ByVal str As String) As String
      lastChar = Mid(str, Len(str), 1)
End Function

===============================================================================

Further, you can check to be sure that a file exists by using the FileExists function.  It returns True if the file exists.  The following code shows how to use the FileExists function:

Dim fileName As Object
Set fileName = CreateObject("Scripting.FileSystemObject")

If fileName.FileExists(App.Path & inputFileName) . . .