Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

i/o operations

What are the file I/O operations in Euphoria?



Files can be written to and read from in Euphoria as it is done in other programming languages. This is basically all that is done when it concerns operations that can be performed on files. There are good number of operations which files in Euphoria can be subjected to. These operations can either write to or read from an existing file or one they will create if it does not exist. These operations can be done using the following methods which are inbuilt in Euphoria.

  • open()
  • close()
  • printf()
  • gets()


This method will open the file so that it can be available for writing and reading.


  • The file’s name.
  • The mode of access of the said file. For example, let’s work on a file named hello.txt:
integer holder
holder = open("hello.txt","w")

This opens the hello.txt file in the write mode.

The file reference object holder was declared as an integer because the return value of open() is 1 on success with the file being saved in the reference object and -1 on failure.

Be sure to provide the direct path to the file.

The possible access mode available includes:

  • w: File will be opened to be written to only. This mode overwrites the indicated file if it exists. If the file does not exist, creates a new file for writing.

  • a: File opened in this mode can only be appended to. Appending will start at end of the file. If the file does not exist, it creates a new file for writing.

  • u: File will be Opened for both reading and writing. This reading and writing will start at the beginning of the file.

  • r: Opens a text file in read only mode. The reading starts at the beginning of file content.

The close() Method

The close() method will flush from memory any unwritten information and then finally closes the file as well. When this is done no writing or reading can be performed on the file again. The only parameter required is the reference object of the file to be closed.

In a situation where this reference object is reassigned to another file, Euphoria will close the file automatically. It is advisable to always close your files properly after using it. For example closing our earlier opened file hello.txt that is now referenced in holder:



The printf() method will print strings to an open file.

printf(fn, st, x) 


  1. fn: The object that references the file gotten from the open() method.

  2. st: This parameter is a string that indicates the format to print in. where decimal atoms will be formatted using %d, while sequences are formatted as strings using %s.

  3. x : If x is a sequence, then format specifiers from the parameter st are matched with corresponding elements of x. If x is an atom, then normally st will have just one format specifier which will be applied to x. However, if st contains multiple format specifiers, then each one is applied to the same value. Example:

integer holder
constant ERROR = 2
constant STDOUT = 1

holder = open("hello.txt", "w")

if holder = -1 then
   puts(ERROR, "couldn't open hello\n")
   puts(STDOUT, "File opened successfully\n")
end if

printf(holder, "My pet is %s and weighs  %d\n", {"friendly",10})

if holder = -1 then
   puts(ERROR, "No need to close the file\n")
   close( holder )
   puts(STDOUT, "File closed successfully\n")
end if

In the example above, a file is open in write mode. Then, some data is written to it using printf, but that is only after a check has been conducted to confirm that the file was opened successfully. The output is given below: Output

File opened successfully
File closed successfully

Meanwhile, if we open the hello.txt file that was initially empty, it should contain this :

My pet is friendly and weighs 10

gets() Method

The Euphoria gets() method reads a string from an open file. To use this method, the file has to be opened in a mode that permits reading from it. This methods accepts one parameter, which is the reference object of the open file which will be read.

For example, let read from our hello.txt file which now has some content.

object content

-- open a file
integer holder

holder = open("hello.txt","r")
--read from it and save in an object
content = gets(holder)

--close the file

-- display content of object
printf(1,"The file contains: %s", {content})

The code above will read from the hello.txt file using the gets() method. Whatever was gathered by the method was saved in the content object variable and printed to the screen using printf() method.


i/o operations

View all Courses

Keep Exploring