FileGet, FileGetObject Procedures | |
Class Microsoft.VisualBasic.FileSystem Syntax FileGet (fileNumber, value[, recordNumber[, dataFlag]]) FileGetObject (fileNumber, value[, recordNumber[, dataFlag]])
fileNumber (required; Integer) Any valid file number of a file opened with FileOpen.
value (required; multiple data types) Variable in which to place retrieved data. May be one of the following data types: Object, Short, Integer, Single, Double, Decimal, Byte, Boolean, Date, System.Array, or String.
recordNumber (optional; Integer) The 1-based location at which reading begins, either a record number (for Random mode) or a byte number (for Binary mode). If omitted, it defaults to 1, which indicates the next available record in the file should be used.
dataFlag (optional; Boolean) New in 2005. This flag is only used when value is of type System.Array or String. For System.Array data, the flag indicates whether the array is dynamic (true) or not (False). For strings, the flag indicates whether the string is fixed in size (TRue) or not (False). If omitted, this field defaults to False. Description The FileGet and FileGetObject procedures read data from an open file into a variable. For files open in Random mode, the data is read from a record position. For Binary files, the data is read from a byte position in the file. FileGet and FileGetObject are identical in functionality, but using FileGetObject may reduce compile-time data-conversion issues when working with Object data values. Usage at a Glance The number of bytes read is governed by the data type of value. When strings are written using FilePut or FilePutObject, a length descriptor is included in the output, unless dataFlag is set to true, which writes out a fixed-length string. When reading back such fixed-length strings, set the dataFlag argument to true and preload the string with the right number of characters before calling this function. 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 oneChar As Char Dim fileID As Integer = FreeFile( ) FileOpen(fileID, "c:\data.txt", OpenMode.Binary, OpenAccess.Read) Do While (Loc(fileID) <> LOF(fileID)) FileGet(fileID, oneChar) ' ----- Do something with oneChar... Loop FileClose(fileID) FileGet is most commonly used to read data from files written with the FilePut function. Version Differences The FileGet and FileGetObject procedures are new with .NET. They are replacements for the Get statement in VB 6, which has a syntax similar to that of FileGet. The dataFlag argument in both the FileGet and the FileGetObject functions is new with Visual Basic 2005. In Visual Basic 2005, the My.Computer.FileSystem object provides more robust access to file-management features. See Also FileOpen Procedure, FilePut, FilePutObject Procedures |