File manipulation

This page documents the File type, which can be used to read and write text files. File is non-clonable.

General concepts

This type provides functions to read and create files. The default encoding is UTF-8. Other supported included are UTF-16 and UTF-32 (read-only).

Global functions

read_file(path)

Return the content of the file named path as a string.

Methods

class File
init(path as String)

Opens the file named path and returns a handle to it. The file is opened for reading: Phonometrica will try to guess the encoding and will default to UTF-8 otherwise.


init(path as String, mode as String)

Opens the file named path and returns a handle to it. The option mode must be one of the following strings:

  • "r" = open the file in reading mode, starting at the beginning of the file (the file must exist)

  • "w" = open the file in writing mode, starting at the beginning of the file (the file is overwritten if it already exists)

  • "a" = open the file in reading mode, starting at the end of the file (the file is created if it doesn’t exist)

  • "r+" = open the file in reading and writing mode, starting at the beginning of the file (the file must exist)

  • "w+" = open the file in reading and writing mode, starting at the beginning of the file (the file is overwritten if it already exists)

  • "a+" = open the file in reading and writing mode, starting at the end of the file (the file is created if it doesn’t exist)

In reading mode, Phonometrica will try to guess the encoding and will default to UTF-8 otherwise. In writing mode, Phonometrica will always use UTF-8.

Functions


close(file as File)

Closes the file. Once the file is closed, no further reading or writing operations are allowed. In general, you don’t need to call this function since a file is automatically closed as soon as the last reference to it released.


eof(file as File)

Returns true if the cursor is positionned at the end of the file, false otherwise.


open(path as String)

Opens the file named path and returns a handle to it. The file is opened for reading: Phonometrica will try to guess the encoding and will default to UTF-8 otherwise.


open(path as String, mode as String)

Opens the file named path and returns a handle to it. The option mode must be one of the following strings:

  • "r" = open the file in reading mode, starting at the beginning of the file (the file must exist)

  • "w" = open the file in writing mode, starting at the beginning of the file (the file is overwritten if it already exists)

  • "a" = open the file in reading mode, starting at the end of the file (the file is created if it doesn’t exist)

  • "r+" = open the file in reading and writing mode, starting at the beginning of the file (the file must exist)

  • "w+" = open the file in reading and writing mode, starting at the beginning of the file (the file is overwritten if it already exists)

  • "a+" = open the file in reading and writing mode, starting at the end of the file (the file is created if it doesn’t exist)

In reading mode, Phonometrica will try to guess the encoding and will default to UTF-8 otherwise. In writing mode, Phonometrica will always use UTF-8.


rewind(file as File)

Rewinds the cursor to the beginning of the file.


len(file as File)

Returns the number of bytes in the file.


write(file as File, text as String)

Writes text to file.


write_line(file as File, text as String)

Writes text to file, and appends a new line separator.


write_lines(file as File, lines as List)

Writes each string in lines to file, and appends a new line separator after each of them.


read_line(file as File)

Reads a line from file. If the cursor is at the end of the file, it returns a empty string.


read_lines(file as File)

Returns the content of the file as a list whose elements are the lines of the file.


seek(file as File, pos as Integer)

Sets the position of the cursor in the file to pos.


tell(file as File)

Returns the current position of the cursor in the file.

Fields

length

Returns the number of bytes in the file.

See also: len(),