TEIMSI
Developer's platform of programming text codes
Home|Utilities|Forum|Documentation

Table of contents -> Chapter 12 - Functions for files


The next topic is the description for the use of the following predefined TEIMSI functions:


1)- binary_read(path)

2)- binary_write(path,string)

3)- closeh(handle)

4)- deletefile(path)

5)- fileexists(path)

6)- filesize(path)

7)- getatt(path)

8)- openrand(path)

9)- openread(path)

10)- openwrite(path)

11)- read(handle,amount)

12)- seek(handle,position)

13)- setatt(path,value)

14)- seteof(handle)

15)- write(handle,string)




binary_read

The "binary_read" function reads all the content of a file loading it into a TEIMSI variable with a data type of string.


Syntax
binary_read(string)
 

Parameters
string
TEIMSI variable with a data type of string. It indicates the path to the file.
 

Returned value
string/null
TEIMSI variable with a data type of string. If the file does not exists then returns "null" (which is "equal" to the "false" boolean).
 

Example:



	var string=binary_read("file.txt")
	alert("Is there a file ?, Answer = " + (string!=false))
	alert(string)



See also <<binary_write>>, <<fileexists>>


Go to top

binary_write

The "binary_write" function write (overrides if already exists) into a file all the content of a string variable.


Syntax
binary_write(path,string)
 

Parameters
path
TEIMSI variable with a data type of string. It indicates the path to the file.
 
string
TEIMSI variable with a data type of string. It's the data to be written to the file.
 

Returned value
boolean
TEIMSI variable with a data type of boolean (true or false) that indicates the result of the function. If "false" is returned, functions "err_number" and "err_description" can be used to get the error's description.
 

Example:



	var filename="file.txt"

	if(binary_write(filename, "Square root of 77= "+sqrt(77))){
		openapp(filename)
	}else{
		alert("Error "+err_number()+_nl+err_description(err_number()))
	}



See also <<binary_read>>, <<err_description>>, <<err_number>>, <<openapp>>


Go to top

closeh

The "closeh" function closes a file whose handler (integer) was returned by any of the following functions: openrand, openread, openwrite.


Syntax
closeh(handle)
 

Parameters
handle
TEIMSI variable with data type of a 32 bits integer number. It contains the handle of file (that is, the internal index from the operating system).
 

Returned value
boolean
TEIMSI variable with a data type of boolean (true or false) that indicates the result of the function. If "false" is returned, functions "err_number" and "err_description" can be used to get the error's description.
 

Example:



	var filename="file.txt"
	var handle=openwrite(filename)
	if(handle==false){
		alert("Error "+err_number()+_nl+err_description(err_number()))
		sys.quit()
	}
	write(handle,"Data.")
	closeh(handle)


See also <<openrand>>, <<openread>>, <<openwrite>>, <<write>>


Go to top

deletefile

The "deletefile" function deletes a file in the file system.


Syntax
deletefile(path)
 

Parameters
path
TEIMSI variable with a data type of string. It indicates the path to the file.
 

Returned value
boolean
TEIMSI variable with a data type of boolean (true or false) that indicates the result of the function. If "false" is returned, functions "err_number" and "err_description" can be used to get the error's description.
 

See also <<fileexists>>


Go to top

fileexists

The "fileexists" function determines whether a file or directory exists.


Syntax
fileexists(path)
 

Parameters
path
TEIMSI variable with a data type of string. It indicates the path to the file or folder.
 

Returned value
boolean
TEIMSI variable with a data type of boolean (true or false) that indicates the result.
 

Example:



	alert(fileexists("c:\\ "))



See also <<filesize>>


Go to top

filesize

The "filesize" function returns the size of a file in a integer variable or double precision, or -1 if doesn't exists. If the size exceeds 2147483647 bytes the value returned is a double precision number rather than an integer.


Syntax
filesize(path)
 

Parameters
path
TEIMSI variable with a data type of string. It indicates the path to the file.
 

Returned value
decimal_number
Numerical variable with the size or the -1 integer if the file does not exists.
 

See also <<fileexists>>


Go to top

getatt

The "getatt" function load the attributes of a file into an integer with certain bits activated according to the attributes (read, hidden, directory, etc.).


Syntax
getatt(path)
 

Parameters
path
TEIMSI variable with a data type of string. It indicates the path to the file.
 

Returned value
attributes/boolean
TEIMSI variable with data type of a 32 bits integer number. It contains the attributes (readonly, hidden, Etc.) or "false" if the file does not exist. The following constants can be used to evaluate the returned integer: _att_archive, _att_compressed, _att_directory, _att_hidden, _att_normal, _att_readonly, _att_system, _att_temporary.
 

Example:


	var filename="file.txt"

	var result=getatt(filename)

	if(result===false){
		alert("Error "+err_number()+_nl+err_description(err_number()))
	}else{
		alert("The file " + filename + ", has an attribute of read-only = " + ((result & _att_readonly) != 0) );
	}


See also <<setatt>>


Go to top

openrand

The "openrand" function opens a file for random access (read or write).


Syntax
openrand(path)
 

Parameters
path
TEIMSI variable with a data type of string. It indicates the path to the file.
 

Returned value
handle/boolean
TEIMSI variable with data type of a 32 bits integer number. It contains the handle of file (that is, the internal index from the operating system). If "false" is returned, functions "err_number" and "err_description" can be used to get the error's description.
 

Example:


	var filename="file.txt"
	var result1=binary_write(filename, "abcdefghij")

	if(result1){
		var handle=openrand(filename)
	}
	if((result1==false) || (handle==false)){
		alert("Error "+err_number()+_nl+err_description(err_number()))
		sys.quit()
	}
		seek(handle,3)
		var letter=read(handle,1)
		closeh(handle)

	alert("Readen letter = "+letter)		// Shows the letter "d"


See also <<openread>>


Go to top

openread

The "openread" function opens a file for read access.


Syntax
openread(path)
 

Parameters
path
TEIMSI variable with a data type of string. It indicates the path to the file.
 

Returned value
handle/boolean
TEIMSI variable with data type of a 32 bits integer number. It contains the handle of file (that is, the internal index from the operating system). If "false" is returned, functions "err_number" and "err_description" can be used to get the error's description.
 

See also <<openwrite>>


Go to top

openwrite

The "openwrite" function opens a file for write access.


Syntax
openwrite(path)
 

Parameters
path
TEIMSI variable with a data type of string. It indicates the path to the file.
 

Returned value
handle/boolean
TEIMSI variable with data type of a 32 bits integer number. It contains the handle of file (that is, the internal index from the operating system). If "false" is returned, functions "err_number" and "err_description" can be used to get the error's description.
 

See also <<openread>>


Go to top

read

The "read" function reads data from a file whose handler (integer) was returned by any of the following functions: "openrand" or "openread".


Syntax
read(handle,amount)
 

Parameters
handle
TEIMSI variable with data type of a 32 bits integer number. It contains the handle of file (that is, the internal index from the operating system).
 
amount
TEIMSI variable with data type of a 32 bits integer number. It indicates how many bytes to read.
 

Returned value
boolean
TEIMSI variable with a data type of boolean (true or false) that indicates the result of the function. If "false" is returned, functions "err_number" and "err_description" can be used to get the error's description.
 

Example:


	(see the example of the "openrand" function)



See also <<write>>, <<openrand>>


Go to top

seek

The "seek" function moves the pointer read/write to a file whose handler (integer) was returned by any of the following functions: openrand, openread, openwrite.


Syntax
seek(handle,offset)
 

Parameters
handle
TEIMSI variable with data type of a 32 bits integer number. It contains the handle of file (that is, the internal index from the operating system).
 
offset
TEIMSI variable with data type of a 32 bits integer number. It indicates the byte offset from the beginning of the file.
 

Returned value
boolean
TEIMSI variable with a data type of boolean (true or false) that indicates the result of the function. If "false" is returned, functions "err_number" and "err_description" can be used to get the error's description.
 

Example:


	(see the example of the "openrand" function)


See also <<seteof>>, <<closeh>>, <<openrand>>


Go to top

setatt

The "setatt" function sets the attributes of a file using an integer with certain activated according to the same attributes (read, hidden, directory, etc.) bits.


Syntax
setatt(path, attributes)
 

Parameters
path
TEIMSI variable with a data type of string. It indicates the path to the file.
 
attributes
TEIMSI variable with data type of a 32 bits integer number. It contains the attributes (readonly, hidden, Etc.) (the value previously returned by the "getatt" function).
 

Returned value
boolean
TEIMSI variable with a data type of boolean (true or false) that indicates the result of the function. If "false" is returned, functions "err_number" and "err_description" can be used to get the error's description.
 

See also <<getatt>>


Go to top

seteof

The "seteof" function sets the end of the file under the current position of the pointer for writing a file. This pointer is automatically updated when writing data to a file or changed by calling the "seek" function explicitly.


Syntax
seteof(handle)
 

Parameters
handle
TEIMSI variable with data type of a 32 bits integer number. It contains the handle of file (that is, the internal index from the operating system).
 

Returned value
boolean
TEIMSI variable with a data type of boolean (true or false) that indicates the result of the function. If "false" is returned, functions "err_number" and "err_description" can be used to get the error's description.
 

See also <<openwrite>>, <<seek>>


Go to top

write

The "write" function writes data to a file whose handler (integer) was returned by any of the following functions: "openrand" or "openwrite".


Syntax
write(handle,string)
 

Parameters
handle
TEIMSI variable with data type of a 32 bits integer number. It contains the handle of file (that is, the internal index from the operating system).
 
string
TEIMSI variable with a data type of string. That is the block of bytes to be written.
 

Returned value
boolean
TEIMSI variable with a data type of boolean (true or false) that indicates the result of the function. If "false" is returned, functions "err_number" and "err_description" can be used to get the error's description.
 

Example:


	(see the example of the "closeh" function)


See also <<read>>, <<closeh>>


Go to top