VB6 File Handling

 Visual Basic Quick Sort

Sub QuickSort(ByRef xx() As Double)
    Call QSort(xx, LBound(xx), UBound(xx))
End Sub
   
Sub QSort(ByRef xx() As Double, Lefti As Integer, Righti As Integer)
    Dim i As Integer, j As Integer, pivote As Double, t As Double
    If Lefti >= Righti Then Exit Sub
    pivote = xx(Lefti)
    i = Lefti
    j = Righti
    While (i < j)
       While xx(i) <= pivote And i < Righti
          i = i + 1
       Wend
       While xx(j) >= pivote And j > Lefti
          j = j - 1
       Wend
       If (i < j) Then
          t = xx(i)
          xx(i) = xx(j)
          xx(j) = t
       End If
     Wend
     t = xx(Lefti)
     xx(Lefti) = xx(j)
     xx(j) = t
     QSort xx, Lefti, j - 1
     QSort xx, j + 1, Righti
End Sub

Visual Basic 6 File Handling

The Line Input # statement Reads a single line from an open sequential file and assigns it to a String variable.
Line Input #filenumber, varname
filenumber    Required. Any valid file number.

varname    Required. Valid Variant or String variable name.

The Input # statement Reads data from an open sequential file and assigns the data to variables.
Input #filenumber, varlist
filenumber    Required. Any valid file number.
varlist    Required. Comma-delimited list of variables that are assigned values read from the file — can't be an array or object variable. However, variables that describe an element of an array or user-defined type may be used.
Data read with Input # is usually written to a file with Write #. Use this statement only with files opened in Input or Binary mode. Double quotation marks (" ") within input data are ignored.
Data items in a file must appear in the same order as the variables in varlist and match variables of the same data type. Using Write # ensures each separate data field is properly delimited.
Dim MyString, MyNumber
Open "TESTFILE" For Input As #1   ' Open file for input.
Do While Not EOF(1)   ' Loop until end of file.
   Input #1, MyString, MyNumber   ' Read data into two variables.
   Debug.Print MyString, MyNumber   ' Print data to the Immediate window.
Loop
Close #1   ' Close file.

Print # Statement Writes display-formatted data to a sequential file.

Print # Statement Writes display-formatted data to a sequential file.
Print #filenumber, [outputlist]

filenumber    Required. Any valid file number.   
The outputlist argument settings are:        [{Spc(n) | Tab[(n)]}] [expression] [charpos]

Spc(n)    Used to insert space characters in the output, where n is the number of space characters to insert.      
Tab(n)    Used to position the insertion point to an absolute column number, where n is the column number. Use Tab with no argument to position the insertion point at the beginning of the next print zone.      
expression    Numeric expressions or string expressions to print.      
charpos    Specifies the insertion point for the next character. Use a semicolon to position the insertion point immediately after the last character displayed. Use Tab(n) to position the insertion point to an absolute column number. Use Tab with no argument to position the insertion point at the beginning of the next print zone. If charpos is omitted, the next character is printed on the next line.   
Open "TESTFILE" For Output As #1   ' Open file for output.
Print #1, "This is a test"   ' Print text to file.
Print #1,   ' Print blank line to file.
Print #1, "Zone 1"; Tab ; "Zone 2"   ' Print in two print zones.
Print #1, "Hello" ; " " ; "World"   ' Separate strings with space.
Print #1, Spc(5) ; "5 leading spaces "   ' Print five leading spaces.
Print #1, Tab(10) ; "Hello"   ' Print word at column 10.
Dim MyBool, MyDate, MyNull, MyError
MyBool = False : MyDate = #February 12, 1969# : MyNull = Null
MyError = CVErr(32767)
Print #1, MyBool ; " is a Boolean value"
Print #1, MyDate ; " is a date"
Print #1, MyNull ; " is a null value"
Print #1, MyError ; " is an error value"
Close #1   ' Close file.
Write # Statement - Writes data to a sequential file.
Write #filenumber, [outputlist]

filenumber    Required. Any valid file number.      
outputlist    Optional. One or more comma-delimited numeric expressions or string expressions to write to a file.   
Open "TESTFILE" For Output As #1   ' Open file for output.
Write #1, "Hello World", 234   ' Write comma-delimited data.
Write #1,   ' Write blank line.
Dim MyBool, MyDate, MyNull, MyError
' Assign Boolean, Date, Null, and Error values.
MyBool = False : MyDate = #February 12, 1969# : MyNull = Null
MyError = CVErr(32767)
' Boolean data is written as #TRUE# or #FALSE#. Date literals are
' written in universal date format, for example, #1994-07-13#
'represents July 13, 1994. Null data is written as #NULL#.
' Error data is written as #ERROR errorcode#.
Write #1, MyBool ; " is a Boolean value"
Write #1, MyDate ; " is a date"
Write #1, MyNull ; " is a null value"
Write #1, MyError ; " is an error value"
Close #1   ' Close file.

Open Statement - Enables input/output (I/O) to a file.
Open pathname For mode [Access access] [lock] As [#]filenumber [Len=reclength]

pathname     String expression that specifies a file name — may include directory or folder, and drive.      
mode     Keyword specifying the file mode: Append, Binary, Input, Output, or Random. If unspecified, the file is opened for Random access.      
access     Keyword specifying the operations permitted on the open file: Read, Write, or Read Write.      
lock     Keyword specifying the operations restricted on the open file by other processes: Shared, Lock Read, Lock Write, and Lock Read Write.      
filenumber     A valid file number in the range 1 to 511, inclusive. Use the FreeFile function to obtain the next available file number.      
reclength     Number less than or equal to 32,767 (bytes). For files opened for random access, this value is the record length. For sequential files, this value is the number of characters buffered.   
You must open a file before any I/O operation can be performed on it. Open allocates a buffer for I/O to the file and determines the mode of access to use with the buffer. If the file specified by pathname doesn't exist, it is created when a file is opened for Append, Binary, Output, or Random modes. If the file is already opened by another process and the specified type of access is not allowed, the Open operation fails and an error occurs.
The Len clause is ignored if mode is Binary.
Important   In Binary, Input, and Random modes, you can open a file using a different file number without first closing the file. In Append and Output modes, you must close a file before opening it with a different file number.
The following code opens the file TESTFILE in sequential-input mode.
Open "TESTFILE" For Input As #1
Close #1 ' Close before reopening in another mode.
This example opens the file in Binary mode for writing operations only.
Open "TESTFILE" For Binary Access Write As #1
The following example opens the file in Random mode. The file contains records of the user-defined type Record.

Type Record   ' Define user-defined type.
   ID As Integer
   Name As String * 20
End Type

Dim MyRecord As Record   ' Declare variable.
Open "TESTFILE" For Random As #1 Len = Len(MyRecord)
Close #1

This code example opens the file for sequential output; any process can read or write to file.

Open "TESTFILE" For Output Shared As #1

This code example opens the file in Binary mode for reading; other processes can't read file.
 Open "TESTFILE" For Binary Access Read Lock Read As #1



Input Function - Returns String containing characters from a file opened in Input or Binary mode.
Input(number, [#]filenumber)

number    Required. Any valid numeric expression specifying the number of characters to return.      
filenumber    Required. Any valid file number.   
Data read with the Input function is usually written to a file with Print # or Put. Use this function only with files opened in Input or Binary mode. Unlike the Input # statement, the Input function returns all of the characters it reads, including commas, carriage returns, linefeeds, quotation marks, and leading spaces. With files opened for Binary access, an attempt to read through the file using the Input function until EOF returns True generates an error. Use the LOF and Loc functions instead of EOF when reading binary files with Input, or use Get when using the EOF function.    Use the InputB function for byte data contained within text files. With InputB, number specifies the number of bytes to return rather than the number of characters to return.
Dim MyChar
Open "TESTFILE" For Input As #1   ' Open file.
Do While Not EOF(1)   ' Loop until end of file.
   MyChar = Input(1, #1)   ' Get one character.
   Debug.Print MyChar   ' Print to the Immediate window.
Loop
Close #1   ' Close file.

FreeFile Function - Returns an Integer representing the next available for use by the Open statement.
FreeFile[(rangenumber)]
rangenumber argument is a Variant that specifies the range from which the next free file number is to be returned. Specify a 0 (default) to return a file number in the range 1 – 255, inclusive. Specify a 1 to return a file number in the range 256 – 511.
Close Statement - Concludes input/output (I/O) to a file opened using the Open statement.
Close [filenumberlist]
The optional filenumberlist argument can be one or more file numbers using the following syntax, where filenumber is any valid file number: [[#]filenumber] [, [#]filenumber] . . .
Reset Statement - Closes all disk files opened using the Open statement.

Put Statement - Writes data from a variable to a disk file.
Put [#]filenumber, [recnumber], varname

filenumber    Required. Any valid file number.      
recnumber     Variant (Long). Record number (Random mode files) or byte number (Binary mode files) at which writing begins.      
varname     Name of variable containing data to be written to disk.   
Data written with Put is usually read from a file with Get.
The first record or byte in a file is at position 1, the second record or byte is at position 2, and so on. If you omit recnumber, the next record or byte after the last Get or Put statement or pointed to by the last Seek function is written. You must include delimiting commas, for example:

Put #4,,FileBuffer
VarString$ = String$(10," ")
Put #1,,VarString$
Type Record   ' Define user-defined type.
   ID As Integer
   Name As String * 20
End Type

Dim MyRecord As Record, RecordNumber   ' Declare variables.
Open "TESTFILE" For Random As #1 Len = Len(MyRecord)
For RecordNumber = 1 To 5   ' Loop 5 times.
   MyRecord.ID = RecordNumber   ' Define ID.
   MyRecord.Name = "My Name" & RecordNumber   ' Create a string.
   Put #1, RecordNumber, MyRecord   ' Write record to file.
Next RecordNumber
Close #1   ' Close file.

Get Statement - Reads data from an open disk file into a variable.
Get [#]filenumber, [recnumber], varname
The Get statement syntax has these parts:

filenumber    Required. Any valid file number.      
recnumber    Optional. Variant (Long). Record number (Random mode files) or byte number (Binary mode files) at which reading begins.      
varname    Required. Valid variable name into which data is read.   
Data read with Get is usually written to a file with Put.
The first record or byte in a file is at position 1, the second record or byte is at position 2, and so on. If you omit recnumber, the next record or byte following the last Get or Put statement (or pointed to by the last Seek function) is read. You must include delimiting commas, for example:    Get #4,,FileBuffer
VarString = String(10," ")
Get #1,,VarString
Type Record   ' Define user-defined type.
   ID As Integer
   Name As String * 20
End Type
Dim MyRecord As Record, Position   ' Declare variables.
Open "TESTFILE" For Random As #1 Len = Len(MyRecord)
Position = 3   ' Define record number.
Get #1, Position, MyRecord   ' Read third record.
Close #1   ' Close file.

Seek Function - Returns a Long specifying the current read/write position within a file opened
Seek(filenumber)
Seek returns a value between 1 and 2,147,483,647 (equivalent to 2^31 – 1), inclusive.

Random    Number of the next record read or written      
Binary,Output, Append,Input :  Byte position at which the next operation takes place. The first byte in a file is at position 1, the second byte is at position 2, and so on.   
Type Record   ' Define user-defined type.
   ID As Integer
   Name As String * 20
End Type
For files opened in Random mode, Seek returns number of next record.
Dim MyRecord As Record   ' Declare variable.
Open "TESTFILE" For Random As #1 Len = Len(MyRecord)
Do While Not EOF(1)   ' Loop until end of file.
   Get #1, , MyRecord   ' Read next record.
   Debug.Print Seek(1)   ' Print record number to the
         ' Immediate window.
Loop
Close #1   ' Close file.
For files opened in modes other than Random mode, Seek returns the byte position at which the next operation takes place. Assume TESTFILE is a file containing a few lines of text.
Dim MyChar
Open "TESTFILE" For Input As #1   ' Open file for reading.
Do While Not EOF(1)   ' Loop until end of file.
   MyChar = Input(1, #1)   ' Read next character of data.
   Debug.Print Seek(1)   ' Print byte position to the
         ' Immediate window.
Loop
Close #1   ' Close file.


Loc Function : Returns a Long specifying the current read/write position within an open file.
Loc(filenumber)       The required filenumber argument is any valid Integer file number.

Random    Number of the last record read from or written to the file.      
Sequential    Current byte position in the file divided by 128. However, information returned by Loc for sequential files is neither used nor required.      
Binary    Position of the last byte read or written.   
Dim MyLocation, MyLine
Open "TESTFILE" For Binary As #1       ' Open file just created.
Do While MyLocation < LOF(1)           ' Loop until end of file.
   MyLine = MyLine & Input(1, #1)       ' Read character into variable.
   MyLocation = Loc(1)               ' Get current position within file.
   Debug.Print MyLine; Tab; MyLocation    ' Print to the Immediate window.
Loop
Close #1                       ' Close file.

EOF Function : Returns an Integer containing the Boolean value True when the end of a file has been reached.
EOF(filenumber) - The required filenumber argument is an Integer containing any valid file number.
Use EOF to avoid the error generated by attempting to get input past the end of a file.

LOF Function : returen size in  bytes, of a file opened using the Open statement.
LOF(filenumber)
The required filenumber argument is an Integer containing a valid file number.
Note   Use the FileLen

FileLen Function - function to obtain the length of a file that is not open.
FileLen(pathname) pathname specifies a file, may include the directory or folder, and the drive.
GetAttr Function - Returns an Integer representing the attributes of a file, directory, or folder.
GetAttr(pathname)  -value returned by GetAttr is the sum of the following attribute values:
vbNormal    0    Normal.            vbReadOnly    1    Read-only.
vbHidden    2    Hidden.            vbSystem    4    System file.
vbDirectory    16    Directory or folder.        vbArchive    32    File has changed since last backup.

Result = GetAttr(FName) And vbArchive-A nonzero value is returned if the Archive attribute is set.


Seek Statement - Sets the position for the next read/write operation within a file opened using the Open statement.
Seek [#]filenumber, position

filenumber    Required. Any valid file number.      
position    Required. Number in the range 1 – 2,147,483,647, inclusive, that indicates where the next read/write operation should occur.   
Record numbers specified in Get and Put statements override file positioning performed by Seek.
Performing a file-write operation after a Seek operation beyond the end of a file extends the file. If you attempt a Seek operation to a negative or zero position, an error occurs.
Type Statement - Used at module level to define a user-defined data type containing one or more elements.
 [Private | Public] Type varname
elementname [([subscripts])] As type
[elementname [([subscripts])] As type]
. . .
End Type
Use Dim, Private, Public, ReDim, or Static to declare a variable of a user-defined type.
Type StateData
   CityCode (1 To 100) As Integer   ' Declare a static array.
   County As String * 30
End Type

Dim Washington(1 To 100) As StateData

No comments: