Return to index

file functions

fclose(filehandle)

top

Closes an open file.

Example:

# Example for fclose
try
    fp = fopen("/tmp/test.txt", "w")
    fclose(fp)
catch err
    println "File operation failed"
endtry


bool = feof(filehandle)

top

Check if open file cursor is at end-of-file

Example:

# Example for feof
try
    write_file("/tmp/test.txt", "hello")
    fp = fopen("/tmp/test.txt", "r")
    println feof(fp)
    fclose(fp)
catch err
    println "File operation failed"
endtry


position = fflush(filehandle)

top

Flushes filehandle write buffer to disk.

Example:

# Example for fflush
try
    fp = fopen("/tmp/test.txt", "w")
    fwrite(fp, "hello")
    fflush(fp)
    fclose(fp)
catch err
    println "File operation failed"
endtry


file_mode = file_mode(file_name)

top

Returns the file mode attributes of a given file, or -1 on error.

Example:

# Example for file_mode
println file_mode("/etc/passwd")
println file_mode("/tmp")


integer = file_size(string)

top

Returns the file size, in bytes, of a given file string, or -1 if the file cannot be checked.

Example:

# Example for file_size
f = temp_file("za-size-")
write_file(f, "hello world")
println file_size(f)
println file_size("/etc/passwd")


error_bool = flock(file_handle[,lock_type])

top

Attempts to place a file lock on file_handle.
Lock type can be "r" (read), "w" (write) or "u" (unlock)


Returns true if the file could not be locked.

Example:

# Example for flock
fh = fopen("/tmp/za_flock.txt", "w")
flock(fh)
fclose(fh)


filehandle = fopen(filename,mode)

top

Opens a file and returns a file handle. mode can be either w (write), wa (write-append) or r (read).

Example:

# Example for fopen
try
    fp = fopen("/tmp/test.txt", "w")
    println fp
    fclose(fp)
catch err
    println "File operation failed"
endtry


string = fread(filehandle,delim)

top

Reads a string from an open file until delim is encountered (or end-of-file).

Example:

# Example for fread
try
    write_file("/tmp/test.txt", "hello world")
    fp = fopen("/tmp/test.txt", "r")
    println fread(fp, 5)
    fclose(fp)
catch err
    println "File operation failed"
endtry


position = fseek(filehandle,offset,relativity)

top

Move the current position of reads or writes to an open file.


relativity indicates where the offset is relative to.


(0:start of file,1:current position, 2:end of file) The newly sought position is returned.

Example:

# Example for fseek
write_file("/tmp/za_fseek.txt", "hello")
fh = fopen("/tmp/za_fseek.txt", "r")
fseek(fh, 0, 0)


position = ftell(filehandle)

top

The current read pointer position is returned.

Example:

# Example for ftell
try
    write_file("/tmp/test.txt", "hello world")
    fp = fopen("/tmp/test.txt", "r")
    println ftell(fp)
    fclose(fp)
catch err
    println "File operation failed"
endtry


fwrite(filehandle,string)

top

Writes a string to an open file.

Example:

# Example for fwrite
try
    fp = fopen("/tmp/test.txt", "w")
    println fwrite(fp, "hello")
    fclose(fp)
catch err
    println "File operation failed"
endtry


bool = is_dir(file_name)

top

Returns true if file_name is a directory.

Example:

# Example for is_dir
println is_dir("/tmp")
println is_dir("/etc/passwd")
println is_dir("/nonexistent")


bool = is_file(file_name)

top

Returns true if file_name is a regular file.

Example:

# Example for is_file
println is_file("/etc/passwd")
println is_file("/tmp")
println is_file("/nonexistent")


int = perms(file_name)

top

Returns the file access permissions as an integer.

Example:

# Example for perms
println perms("/etc/passwd")
println perms("/tmp")


string = read_file(string)

top

Returns the contents of the named file string, or errors.

Example:

# Example for read_file
f = temp_file("za-read-")
write_file(f, "hello world")
println read_file(f)


stat_struct = stat(file_name)

top

Returns a unix file stat structure containing underlying file information.

Example:

# Example for stat
println stat("/tmp")


bool = write_file(filename,wstring[,mode_number_or_string])

top

Writes the contents of wstring to file filename. Optionally sets the file permissions on new files. Returns true on success.

Example:

# Example for write_file
write_file("/tmp/test.txt", "hello world")