DADiSP Worksheet Functions > Function Categories > File Manipulation > FREADB

 

FREADB

Purpose:

Reads a binary data file and returns the values as a series.

Syntax:

FREADB("filename", filetype, length, byteswap)

"filename"

-

A string, the name of the source file.

filetype

-

An integer, the binary format type of the data file described by either its name or integer code. Valid arguments are:

 

Name

Code

Data Type

Range

INT8

SBYTE

1

8-bit Signed Byte

-128 to +127

UINT8

UBYTE

BYTE

2

8-bit Unsigned Byte

0 to 255

INT16

SINT

3

16-bit (2-byte) Signed Integer

-32768 to +32767

UINT16

UINT

4

16-bit (2-byte) Unsigned Integer

0 to 65535

INT32

LONG

5

32-bit (4-byte) Signed Integer

-2,147,483,648 to +2,147,483,647

FLOAT

6

32-bit (4-byte) Floating Point

-1037 to +1038

DOUBLE

7

64-bit (8-byte) Floating Point

-10-307 to +10308

UINT32

ULONG

8

32-bit (4-byte) Unsigned Integer

0 to 4,294,967,295

INT64

9

64-bit (8-byte) Signed Integer

-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807

UINT64

10

64-bit (8-byte) Unsigned Integer

0 to 18,446,744,073,709,551,615

 

1000 + N

N-byte Signed Integer

-28N-1 to 28N-1-1

 

2000 + N

N-byte Unsigned Integer

0 to 28N-1

 

length

-

Optional. An integer. The number of data points to read. If length is set to -1, the entire file is read. Defaults to -1.

byteswap

-

Optional. An integer. Swap the order of the bytes read.

0:

do not swap bytes (default)

1:

swap bytes

Returns:

A series.

Example:

writeb("mix.dat", float, 1, {1.5, 2.5, 3.5})

writeb("mix.dat", sint, 2, {10, -20})

 

fopen("mix.bin", "rb")

a = freadb("mix.bin", float, 3);

b = freadb("mix.bin", sint, 2)

fclose("mix.bin")

 

a == {1.5, 2.5, 3.5}

b == {10, -20}

 

The WRITEB function creates a file that contains 3 float values and then appends 2 signed integer values. FREADB reads the values into the series variables.

Example:

fopen("mix.bin", "rb")

fseek("mix.bin", 8, 0)

c = castreal(freadb("mix.bin", float, 1));

fclose("mix.bin")

 

c == 3.5

 

The FSEEK positions the file at the third float value by skipping the first 8 bytes. FREADB reads the float value as a one point series. The CASTREAL function converts the series into a real scalar.

Example:

fopen("speech.dat", "rb")

dbser = freadb("speech.dat", DOUBLE, -1)

fcloseall

 

returns a series that contains all the data in speech.dat read as doubles.

Remarks:

File must be opened with FOPEN before using FREADB. Use the "rb" mode to open a binary file in read mode.

 

FREADB is like READB once FOPEN is used to open the file.

 

FREADB always returns a series. To return a scalar, use GETPT or the CAST functions. For example, to read a single integer from a file:

 

fopen("test.dat", "rb")

iser = freadb("test.dat", SINT, 1)

ival = castint(iser)

See Also:

CASTBYTE

CASTINTEGER

CASTREAL

FCLOSE

FCLOSEALL

FOPEN

FREADA

FWRITEB

READB

WRITEB