Revision [14000]

This is an old revision of KeyPgGetfileio made by DoS386 on 2008-12-01 04:55:56.


GET (File I/O)

Reads data from a file to a buffer

Get #filenum, position, data [, [amount] [, bytesread ] ]
varres = Get (#filenum, position, data [, [amount] [, bytesread ] ] )

The value passed to KeyPgOpen Open when the file was opened
The position where the read must start. If the file was opened KeyPgRandom For Random, the position is in records; otherwise, it is in bytes. If omitted, read starts at the present file pointer position. The position is 1-based: i.e. first record or byte of a file is at position 1.
The buffer where data is written. It can be a numeric variable, a string, an array, a user defined type or a dereferenced pointer. The read operation will try to fill completely the variable, unless the KeyPgEof EOF is reached. When getting arrays, data should be followed by an empty pair of brackets: "()". Get will write all of the data in the array. amount is not allowed.
Note: If you want to read values into a buffer, you should NOT pass a pointer to the buffer; instead you should pass the first variable in the buffer. (This can be done by dereferencing the pointer with KeyPgOpValueOf Operator * (Value of).) Get will automatically infer that the rest of the buffer continues after the first variable. If you pass a pointer directly, then Get will overwrite the pointer variable, not the memory it points to.
Limits Get to read read amount * KeyPgSizeof Sizeof(datatype) bytes of data. If amount is omitted it defaults to 1, and Get just reads a single variable of the buffer's data type.
An unsigned integer variable to accept the result of the number of bytes read successfully from the file.

Return Value:
Zero (0) on success; non-zero on error. Note: if KeyPgEof EOF (end of file) is reached while reading, Get will return success. The amount of bytes actually read can be checked by passing a bytesread variable.

Reads binary data from a file to a buffer variable
Get can be used as a function, and will return 0 on success or an error code on failure.
For files opened in KeyPgRandom Random mode, the size in bytes of the data to read must match the specified record size.

Dim Shared f As Integer

Sub get_integer()

    Dim buffer As Integer ' Integer variable

    ' Read an Integer (4 bytes) from the file into buffer, using file number "f".
    Get #f, , buffer

    ' print out result
    Print buffer

End Sub

Sub get_array()

    Dim an_array(0 To 10-1) As Integer ' array of Integers

    ' Read 10 Integers (10 * 4 = 40 bytes) from the file into an_array, using file number "f".
    Get #f, , an_array()

    ' print out result
    For i As Integer = 0 To 10-1
        Print an_array(i)

End Sub

Sub get_mem

    Dim pmem As Integer Ptr

    ' allocate memory for 5 Integers
    pmem = Allocate(5 * SizeOf(Integer))

    ' Read 5 integers (5 * 4 = 20 bytes) from the file into allocated memory
    Get #f, , *pmem, 5 ' Note pmem must be dereferenced (*pmem, or pmem[0])

    ' print out result using [] Pointer Indexing
    For i As Integer = 0 To 5-1
        Print pmem[i]

    ' free pointer memory to prevent memory leak
    Deallocate pmem

End Sub

' Find the first free file file number.
f = FreeFile

' Open the file "file.ext" for binary usage, using the file number "f".
Open "file.ext" For Binary As #f




' Close the file.  
Close #f

' Load a small text file to a string

Function LoadFile(ByRef filename As String) As String
    Dim h As Integer
    Dim txt As String
    h = FreeFile
    If Open( filename For Binary Access Read As #h ) <> 0 Then Return ""
    If LOF(h) > 0 Then
        txt = String(LOF(h), 0)
        If Get( #h, ,txt ) <> 0 Then txt = ""
    End If
    Close #h
    Return txt
End Function

Dim ExampleStr As String
ExampleStr = LoadFile("smallfile.txt")
Print ExampleStr

Differences from QB:
See also:
Back to File I/O Functions

Valid XHTML :: Valid CSS: :: Powered by WikkaWiki phatcode