# 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 asNO_WRITE_OPS, which will disable support for writing.

## Public C++ interface

    class CScriptFile
    {
    public:
      // Constructor
      CScriptFile();

      // Memory management
      void AddRef();
      void Release();

      // 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
      CScriptString *ReadString(unsigned int length);

      // Reads to the next new-line character
      CScriptString *ReadLine();

      // Writes a string to the file
      int WriteString(const std::string &str);

      // File cursor manipulation
      int GetPos() const;
      int SetPos(int pos);
      int MovePos(int delta);
    };

## 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();
        int      writeString(const string &in string);
        int      getPos() const;
        int      setPos(int pos);
        int      movePos(int delta);
      }

## 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();
      }