/
readString

readString

Description

Use to read a string from a file. Command will fail if at the end of the file.

The following is a listing of the different cast types. Note that data can be lost when casting. 'Signed' means the value can be negative. 'Unsigned' means value will always be positive.

Casting doesn't make sense when reading strings, but the option is still there for you.

Cast Description
c Reads each string character as a signed character, or signed 8-bit (1 byte) value
uc Reads each string character as an unsigned character, or unsigned 8-bit (1 byte) value
s Reads each string character as a signed short integer, or signed 16-bit (2 bytes) value
us Reads each string character as an unsigned short integer, or unsigned 16-bit (2 bytes) value
i Reads each string character as a signed integer, or signed 32-bit (4 bytes) value
ui Reads each string character as an unsigned integer, or unsigned 32-bit (4 bytes) value
f Reads each string character as a floating point value, or a 32-bit (4 byte) value

Functional area

Disk I/O

Command syntax

Syntax

readString fileID length [-cast string]

Arguments

Name Type Required Comments
length int
Length of the string to read from the file. Must be a positive value.
Will read in characters up to the newline character, or length, whichever comes first.
Will not return the newline character. Maximum value is 1024
fileID int
ID of file previously opened with fileOpen.

Flags

Name Flag arguments Argument type Exclusive to Comments
cast 1 string Data type to cast to. Possible types are listed above. Note that data can be lost when casting.

Return value

string

Examples

string $str; 
int $length;
int $fileID;
int $fileID = `fileOpen "C:/FileTesting.txt" "r"`;
  
// First read the string length
$length = `readInt $fileID` + 1;
  
// Now read the string
$str = `readString $fileID $length`;
  
fileClose $fileID; //close the file
  
// Print out the value
print $str;

Additional information

Related commands