Files

Silk can handle file with the following built-in functions:

Open file
Use _fun("fopen", filename, mode) to create a new file or open an existing file, the first argument is the function name 'fopen', filename is the path of the file we need to open, and mode is the access mode.
access mode can have one of the following values:
  1. r   Opens an existing text file for reading purpose.
  2. w  Opens a text file for writing. If it does not exist, then a new file is created. The content will be written from the beginning of the file.
  3. a   Opens a text file for writing in appending mode. If it does not exist, then a new file is created. The content  will be appended in the existing file content.
  4. r+  Opens a text file for both reading and writing.
  5. w+  Opens a text file for both reading and writing. It first truncates the file to zero length if it exists, otherwise creates a file if it does not exist.
  6. a+   Opens a text file for both reading and writing. It creates the file if it does not exist. The reading will start from the beginning but writing can only be appended.
To handle binary files,  you will use following access modes instead of the above mentioned ones:
"rb", "wb", "ab", "rb+", "r+b", "wb+", "w+b", "ab+", "a+b"
The function will return the file handle if the file is opened successfully, otherwise it will return null.

Close file
Use  _fun("fclose", file) to close the file, the second argument file is the file handle.

File size
Use _fun("fsize", file) to get the file size, the second argument file is the file handle.

Read file
Use _fun("fread", file, size) to get the file data with the specified size. 
The second argument file is the file handle, and the third argument size is the size we need to read.

Write file
Use _fun("fwrite", file, content) to write data to file.
The second argument file is the file handle, and the third argument content is the content we need to write.

File seek
Use _fun("fseek", file, pos) to sets the position of the file to the given offset.
The second argument file is the file handle, and the third argument pos is the file position we need to set.

Remove file
Use _fun("fremove", file) to delete the given file.
The second argument file is the filename to be removed.

Rename file
Use _fun("frename", file, newfile) to rename the file to the newfile name.
The second argument file is the filename to be renamed, and the third argument newfile is the new name of the file.

The following example shows the usage of file functions:
main()
{
    file=_fun("fopen","e:\\test.txt","rb"); //open the file with read binary mode
    if(file)
    {
        print("Open file OK");
        size=_fun("fsize",file);
        print("file size:",size);
       
        _fun("fseek",file,0); //set the position at the begin of the file   
        content=_fun("fread",file,size); //read all the content of the file
        print(content);
           
        _fun("fclose",file); //close the file
    }
}

CFile Class
There is a class CFile which encapsulates most of the file functions in Silk installer, it's easy for users to handle file:
class CFile()
{
    self.file=null;
    self.size=0;
   
    func Open(filename,mode)
    {
        self.file=_fun("fopen",filename,mode);
        self.size=_fun("fsize",self.file);
        _fun("fseek",self.file,0);   
        return self.file;
    }
    func ReadAll()
    {
        if(self.file==null)
            return null;
        return _fun("fread",self.file,self.size);
    }
    func Seek(pos)
    {
        if(self.file==null)
            return null;
        _fun("fseek",self.file,pos);
    }
    func Read(size)
    {
        if(self.file==null)
            return null;
        return _fun("fread",self.file,size);
    }
    func Write(content)
    {
        if(self.file==null)
            return null;
        _fun("fwrite",self.file,content);
   
    }
    func Close()
    {
        if(self.file==null)
            return;
        _fun("fclose",self.file);
        self.file=null;
    }
   
}

The usage of CFile:
#include "include\file.si"
main()
{
    file=CFile();
    if(file.Open("e:\\test.txt","rb"))
    {
        content=file.ReadAll();
        print(content);
    }
    file.Close();
}