READBYTES Function

Reads a certain number of bytes from a file.

Category: External File
Returned data type: Integer
Note: The returned value is the number of bytes actually read, or 0 on failure.

Syntax

<fileobject>.READBYTES(<number_of_bytes, buffer>)

Required Arguments

number_of_bytes

an integer specifying the number of bytes that need to be read from the file. This parameter can be specified as a number, field name, or expression.

buffer

a string that contains the bytes that are read. This parameter can be specified as a fixed string, field name, or expression.

Details

The READBYTES method reads the specified number of bytes from a file starting at the current position of the file pointer. The file pointer will be positioned after the last byte read. If the buffer is too small, only the first bytes from the file are put into the buffer.
This method is normally used to read binary files. The various format functions can be used to convert the binary information that was read.
Note that this method also reads EOL characters. When reading a windows text file like this:
C:\filename.txt
abc
def
A READBYTES(7, buffer) statement causes the field buffer to contain the following value "abc\n de". The value consists of all the information from the first line (3 bytes), followed by a CR character and an LF character (2 bytes on Windows, 1 byte on UNIX) that is represented by "\n". They are followed by the first 2 bytes from the second line. To read text files, use the READLINE() method.

Example

string input
file f
 
f.open("C:\filename.txt", "r")
f.readbytes(7, input)
 
// or if you want to test return codes for the method calls
// boolean rc_ok
// rc_ok = f.open("C:\filename.txt", "r")
// rc_ok = f.readbytes(7, input)