Files
Silk can handle file with the following built-in functions:
Open fileUse
_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:
- r Opens an existing text file for reading purpose.
- 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.
- 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.
- r+ Opens a text file for both reading and writing.
- 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.
- 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 fileUse
_fun("
fclose", file) to close the file, the second argument file is the file handle.
File sizeUse
_fun("
fsize", file) to get the file size, the second argument file is the file handle.
Read fileUse
_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 fileUse
_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 seekUse
_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 fileUse
_fun("
fremove", file) to delete the given file.
The second argument file is the filename to be removed.
Rename fileUse
_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 ClassThere 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();
}