AngelScript
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
file object

Path: /sdk/add_on/scriptfile/

This object provides support for reading and writing files.

Register with RegisterScriptFile(asIScriptEngine*).

If you do not want to provide write access for scripts then you can compile the add on with the define AS_WRITE_OPS 0, which will disable support for writing. This define can be made in the project settings or directly in the header.

Public C++ interface

class CScriptFile
{
public:
// Constructor
CScriptFile();
// Memory management
void AddRef() const;
void Release() const;
// Opening and closing file handles
// mode = "r" -> open the file for reading
// mode = "w" -> open the file for writing (overwrites existing files)
// mode = "a" -> open the file for appending
int Open(const std::string &filename, const std::string &mode);
int Close();
// Returns the size of the file
int GetSize() const;
// Returns true if the end of the file has been reached
bool IsEOF() const;
// Reads a specified number of bytes into the string
std::string ReadString(unsigned int length);
// Reads to the next new-line character
std::string ReadLine();
// Reads a signed integer
asINT64 ReadInt(asUINT bytes);
// Reads an unsigned integer
asQWORD ReadUInt(asUINT bytes);
// Reads a float
float ReadFloat();
// Reads a double
double ReadDouble();
// Writes a string to the file
int WriteString(const std::string &str);
int WriteInt(asINT64 v, asUINT bytes);
int WriteUInt(asQWORD v, asUINT bytes);
int WriteFloat(float v);
int WriteDouble(double v);
// File cursor manipulation
int GetPos() const;
int SetPos(int pos);
int MovePos(int delta);
// Determines the byte order of the binary values (default: false)
// Big-endian = most significant byte first
bool mostSignificantByteFirst;
};

Public script interface

  class file
  {
    int      open(const string &in filename, const string &in mode);
    int      close();
    int      getSize() const;
    bool     isEndOfFile() const;
    string   readString(uint length);
    string   readLine();
    int64    readInt(uint bytes);
    uint64   readUInt(uint bytes);
    float    readFloat();
    double   readDouble();
    int      writeString(const string &in str);
    int      writeInt(int64 value, uint bytes);
    int      writeUInt(uint64 value, uint bytes);
    int      writeFloat(float value);
    int      writeDouble(double value);
    int      getPos() const;
    int      setPos(int pos);
    int      movePos(int delta);
    bool     mostSignificantByteFirst;
  }

int open(const string &in filename, const string &in mode)

Opens a file. The mode can be "r" for reading, "w" for writing, or "a" for appending.

If the file couldn't be opened, a negative value is returned.

int close()

Closes the file.

If no file is open, a negative value is returned.

int getSize() const

Returns the size of the file, or a negative value if no file is open.

bool isEndOfFile() const

Returns true if the current position is at the end of the file.

string readString(uint length)

Reads length bytes into a string and returns it.

string readLine()

Reads until a new line character, e.g. '\n', or end-of-file and returns the string. The new line character is also returned in the string.

int64 readInt(uint bytes)

Reads bytes as a signed integer number.

uint64 readUInt(uint bytes)

Reads bytes as an unsigned integer number.

float readFloat()

Reads 4 bytes as a float number.

double readDouble()

Reads 8 bytes as a double number.

int writeString(const string &in str)

Writes the bytes of the string into the file.

Returns the number of bytes written, or a negative value on error.

int writeInt(int64 value, uint bytes)

Writes bytes as a signed integer value.

Returns the number of bytes written, or a negative value on error.

int writeUInt(uint64 value, uint bytes)

Writes bytes as an unsigned integer value.

Returns the number of bytes written, or a negative value on error.

int writeFloat(float value)

Writes 4 bytes as a float value.

Returns the number of bytes written, or a negative value on error.

int writeDouble(double value)

Writes 8 bytes as a double value.

Returns the number of bytes written, or a negative value on error.

int getPos() const

Returns the current position in the file, or a negative value on error.

int setPos(int pos)

Sets the current position in the file. Returns the previous position or a negative value on error.

int movePos(int delta)

Moves the position delta bytes relative to the current position. Returns the previous position or a negative value on error.

bool mostSignificantByteFirst

This property should be set to true if the most significant bit should be read or written first in the methods that reads/writes numbers.

It is set to false by default, which is the standard on most platforms.

Script example

  file f;
  // Open the file in 'read' mode
  if( f.open("file.txt", "r") >= 0 ) 
  {
      // Read the whole file into the string buffer
      string str = f.readString(f.getSize()); 
      f.close();
  }