FileGet, FileGetObject Procedures |
Microsoft.VisualBasic.FileSystem
FileGet( FileNumber, Value, RecordNumber ) FileGetObject( FileNumber, Value, RecordNumber )
Any valid file number
Variable in which to place file contents
The location at which reading begins
Copies data from a file on disk into a variable
For the FileGet procedure, the variable can have one of the following data types:
For the FileGetObject procedure, the variable must be of type Object.
For files opened in Random mode, RecordNumber refers to the record number in the file.
For files opened in Binary mode, RecordNumber refers to the byte number within the file.
The number of bytes read by the FileGet procedure is governed by the data type of Value . The following is the number of bytes read by each data type:
Data type | Bytes read |
---|---|
Boolean | 2 |
Byte | 1 |
Char | 1 |
Date | 8 |
Decimal | 8 |
Double | 16 |
Integer | 4 |
Long | 8 |
Short | 2 |
Single | 8 |
String | Len( string ) |
Note that the number of bytes read by a String variable depends on the length of the string. Hence, a string must be initialized to the desired size before calling the FileGet procedure.
The position of the first record or byte within a file is always 1.
When a record or a number of bytes is read from a file using FileGet , the file pointer automatically moves to the record or byte following the one just read. You can therefore read all data sequentially from a Random or Binary file by omitting RecordNumber , as this snippet shows:
Dim fr As Integer = FreeFile( ) Dim sChar As Char FileOpen(fr, "c:\data.txt", OpenMode.Binary, OpenAccess.Read) FileGet(fr, sChar, 1) do while loc(fr) <> LOF(fr) FileGet(fr, sChar) ' do something with sChar. . . Loop FileClose(fr)
FileGet is most commonly used to read data from files written with the FilePut function.
This example illustrates the use of the Char data type to read and output each byte of a file:
Public Sub Main Dim fr As Integer = FreeFile( ) Dim sFile As String = Space(FileLen("C:\data.txt")) FileOpen(fr, "c:\data.txt", OpenMode.Binary, OpenAccess.Read) FileGet(fr, sFile) Console.WriteLine(sFile) ' Displays entire file FileClose(fr) End Sub
With the increase in the power, flexibility, and ease of use of modern DBMSs, the use of external standalone data files has fallen dramatically, which means that statements such as FileGet and FileOpen are becoming much less important.
The FileGet and FileGetObject procedures are new to VB.NET. They are replacements for the Get statement in VB 6, whose syntax is similar to that of FileGet .
FileOpen Procedure, FilePut, FilePutObject Procedures