contents   index   previous   next



Clib.fread()

syntax:

Clib.fread(dstVar, varDescription, filePointer)

where:

dstVar - variable to hold data read from file.

 

varDescription - description of the data to read, that is, how and how much.

 

filePointer - pointer to file to use.

 

return:

number - elements read on success, 0 on failure.

 

description:

This method reads data from an open file and stores it in dstVar. If it does not yet exist, dstVar will be created. varDescription is a variable that describes the how and how much data is to be read: if dstVar is a buffer, it will be the length of the buffer; if dstVar is an object, varDescription must be an object descriptor; and if dstVar is to hold a single datum then varDescription must be one of the following.

 

UWORD8
Stored as a byte in dstVar

SWORD8
Stored as an integer in dstVar

UWORD16
Stored as an integer in dstVar

SWORD16
Stored as an integer in dstVar

UWORD24
Stored as an integer in dstVar

SWORD24
Stored as an integer in dstVar

UWORD32
Stored as an integer in dstVar

SWORD32
Stored as an integer in dstVar

FLOAT32
Stored as a float in dstVar

FLOAT64
Stored as a float in dstVar

FLOAT80
Stored as a float in dstVar (not available in Win32)

 

In all cases, this function returns the number of elements read. For dstVar being a buffer, this would be the number of bytes read, up to length specified in varDescription. For dstVar being an object, this method returns 1 if the data is read or 0 if read error or endoffile is encountered.

 

For example, the definition of an object might be:

 

ClientDef.Sex = UWORD8; 

ClientDef.MaritalStatus = UWORD8; 

ClientDef._Unused1 = UWORD16; 

ClientDef.FirstName = 30; ClientDef.LastName = 40; 

ClientDef.Initial = UWORD8;

 

The ScriptEase version of Clib.fread() differs from the standard C version in that the standard C library is set up for reading arrays of numeric values or structures into consecutive bytes in memory. In JavaScript, this is not necessarily the case.

 

Data types will be read from the file in a byteorder described by the current value of the _BigEndianMode global variable.

 

see:

Clib.fopen(), Clib.fwrite()

 

example:

// To read the 16?bit integer "i",

// the 32?bit float "f", and

// then 10 byte buffer "buf"

// from the open file "fp"

// use code like the following.

 

if ( !Clib.fread(i,SWORD16,fp) ||

     !Clib.fread(f,FLOAT32,fp) ||

     (10 != Clib.fread(buf,10,fp)) )

{

   Clib.printf("Error reading from file.\n"); 

   Clib.abort(); 

}

 


Clib.freopen()