FileGet, FileGetObject Procedures

   
FileGet, FileGetObject Procedures

Class

Microsoft.VisualBasic.FileSystem

Syntax

 FileGet(   FileNumber, Value, RecordNumber   ) FileGetObject(   FileNumber, Value, RecordNumber   ) 
FileNumber (required; Integer)

Any valid file number

Value (required; any (see the first two items in Section )

Variable in which to place file contents

RecordNumber (optional; Integer)

The location at which reading begins

Description

Copies data from a file on disk into a variable

Rules at a Glance

  • For the FileGet procedure, the variable can have one of the following data types:

    Array
    Boolean
    Byte
    Char
    Date
    Decimal
    Double
    Integer
    Long
    Short
    Single
    String
  • 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.

Example

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 

Programming Tips and Gotchas

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.

VB.NET/VB 6 Differences

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 .

See Also

FileOpen Procedure, FilePut, FilePutObject Procedures

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net