Euphoria - Files I/O


Advertisements

Using Euphoria programming language, you can write programs that read and change file data on your floppy drive or hard drive, or create new files as a form of output. You can even access devices on your computer such as the printer and modem.

This chapter described all the basic I/O functions available in Euphoria. For information on more functions, please refer to standard Euphoria documentation.

Displaying on the Screen

The simplest way to produce output is using the puts() statement where you can pass any string to be displayed on the screen. There is another method printf() which can also be used in case you have to format a string using dynamic values.

These methods convert the expressions you pass them to a string and write the result to standard output as follows −

#!/home/euphoria-4.0b2/bin/eui
 
puts(1, "Euphoria is really a great language, isn't it?" )

This produces the following result on your standard screen −

Euphoria is really a great language, isn't it?

Opening and Closing Files

Euphoria provides basic methods necessary to manipulate files by default. You can do your most of the file manipulation using the following methods −

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

The open Method

Before you can read or write a file, you have to open it using Euphoria's built-in open()method. This function creates a file descriptor which is utilized to call other supporting methods associated with it.

Syntax

integer file_num = open(file_name, access_mode)

Above method returns -1 in case there is an error in opening the given file name. Here are the parameters −

  • file_name − The file_name argument is a string value that contains the name of the file that you want to access.

  • access_mode − The access_mode determines the mode in which the file has to be opened. For example, read, write append, etc. A complete list of possible values for file opening modes is given in the following table −

S.No Modes & Description
1

r

Opens a text file for reading only. The file pointer is placed at the beginning of the file.

2

rb

Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file.

3

w

Opens a text file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.

4

wb

Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.

5

u

Opens a file for both reading and writing. The file pointer is set at the beginning of the file.

6

ub

Opens a file for both reading and writing in binary format. The file pointer is placed at the beginning of the file.

7

a

Opens a file for appending. The file pointer is at the end of the file if the file exists (append mode). If the file does not exist, it creates a new file for writing.

8

ab

Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists (append mode). If the file does not exist, it creates a new file for writing.

Example

The following example creates a new text file in the current directory on your Linux system −

#!/home/euphoria-4.0b2/bin/eui

integer file_num
constant ERROR = 2
constant STDOUT = 1

file_num = open("myfile,txt", "w")

if file_num = -1 then
   puts(ERROR, "couldn't open myfile\n")
else
   puts(STDOUT, "File opend successfully\n")
end if

If file opens successfully, then it "myfile.txt" is created in your current directory and produces the following result −

File opend successfully

The close() Method

The close() method flushes any unwritten information and closes the file, after which no more reading or writing can be done on the file.

Euphoria automatically closes a file when the reference object of a file is reassigned to another file. It is a good practice to use the close() method to close a file.

Syntax

close( file_num );

Here the file descriptor received while opening a file is passed as a parameter.

Example

The following example creates a file as above and then closes it before existing the program −

#!/home/euphoria-4.0b2/bin/eui

integer file_num
constant ERROR = 2
constant STDOUT = 1

file_num = open("myfile.txt", "w")

if file_num = -1 then
   puts(ERROR, "couldn't open myfile\n")
else
   puts(STDOUT, "File opend successfully\n")
end if

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

This produces the following result −

File opend successfully
File closed successfully

Reading and Writing Files

Euphoria provides a set of access methods to make our lives easier while reading or writing a file either in text mode or binary mode. Let us see how to use printf() and gets() methods to read and write files.

The printf() Method

The printf() method writes any string to an open file.

Syntax

printf(fn, st, x) 

Here are the parameters −

  • fn − File descriptor received from open() method.

  • st − Format string where decimal or atom is formatted using %d and string or sequence is formatted using %s.

  • x − If x is a sequence, then format specifiers from st are matched with corresponding elements of x. If x is an atom, then normally st contains just one format specifier and it is applied to x. However; if st contains multiple format specifiers, then each one is applied to the same value x.

Example

The following example opens a file and writes the name and age of a person in this file −

#!/home/euphoria-4.0b2/bin/eui

integer file_num
constant ERROR = 2
constant STDOUT = 1

file_num = open("myfile.txt", "w")

if file_num = -1 then
   puts(ERROR, "couldn't open myfile\n")
else
   puts(STDOUT, "File opend successfully\n")
end if

printf(file_num, "My name is %s and age is %d\n", {"Zara", 8})

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

The above example creates myfile.txt file. Is writes given content in that file and finally closes. If you open this file, it would have the following content −

My name is Zara and age is 8

The gets() Method

The gets() method reads a string from an open file.

Syntax

gets(file_num)

Here passed parameter is file description return by the opend() method. This method starts reading from the beginning of the file line by line. The characters have values from 0 to 255. The atom -1 is returned on end of file.

Example

Let us take a file myfile.txt which is already created.

#!/home/euphoria-4.0b2/bin/eui

integer file_num
object line

constant ERROR = 2
constant STDOUT = 1

file_num = open("myfile.txt", "r")
if file_num = -1 then
   puts(ERROR, "couldn't open myfile\n")
else
   puts(STDOUT, "File opend successfully\n")
end if

line = gets(file_num)
printf( STDOUT, "Read content : %s\n", {line})

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

This produces the following result −

File opend successfully
Read content : My name is Zara and age is 8

File closed successfully

Euphoria provides a list of many methods which helps you in manipulating files. These methods are listed in Euphoria Library Routines.

Advertisements