📅  最后修改于: 2020-11-04 07:56:03             🧑  作者: Mango
使用Euphoria编程语言,您可以编写程序来读取和更改软盘驱动器或硬盘驱动器上的文件数据,或者创建新文件作为输出形式。您甚至可以访问计算机上的设备,例如打印机和调制解调器。
本章介绍了幸福感中所有可用的基本I / O功能。有关更多功能的信息,请参阅标准Euphoria文档。
产生输出的最简单方法是使用puts()语句,您可以在其中传递要在屏幕上显示的任何字符串。还有另一种方法printf() ,如果必须使用动态值格式化字符串,也可以使用该方法。
这些方法将传递给它们的表达式转换为字符串,并将结果写入标准输出,如下所示:
#!/home/euphoria-4.0b2/bin/eui
puts(1, "Euphoria is really a great language, isn't it?" )
这会在标准屏幕上产生以下结果-
Euphoria is really a great language, isn't it?
Euphoria提供了默认情况下处理文件所必需的基本方法。您可以使用以下方法进行大部分文件操作-
在读取或写入文件之前,您必须使用Euphoria内置的open()方法将其打开。此函数创建一个文件描述符,该文件描述符用于调用与其关联的其他支持方法。
integer file_num = open(file_name, access_mode)
如果打开给定文件名时出现错误,上述方法将返回-1。这是参数-
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. |
以下示例在Linux系统上的当前目录中创建一个新的文本文件-
#!/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
如果文件成功打开,则将在当前目录中创建“ myfile.txt”,并产生以下结果:
File opend successfully
close()方法将刷新所有未写入的信息并关闭文件,此后将无法再对该文件进行读取或写入。
当文件的引用对象重新分配给另一个文件时,欣快感会自动关闭文件。最好使用close()方法关闭文件。
close( file_num );
在这里,打开文件时收到的文件描述符作为参数传递。
以下示例如上所述创建一个文件,然后在存在程序之前将其关闭-
#!/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
这产生以下结果-
File opend successfully
File closed successfully
欣快感提供了一组访问方法,使我们在以文本模式或二进制模式读取或写入文件时更加轻松自如。让我们看看如何使用printf()和gets()方法读取和写入文件。
printf()方法将任何字符串写入打开的文件。
printf(fn, st, x)
这是参数-
以下示例打开一个文件,并在此文件中写出一个人的姓名和年龄-
#!/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
上面的示例创建myfile.txt文件。将给定的内容写入该文件,最后关闭。如果打开此文件,它将具有以下内容-
My name is Zara and age is 8
gets()方法从打开的文件中读取一个字符串。
gets(file_num)
这里传递的参数是opend()方法返回的文件描述。此方法从文件的开头开始逐行读取。字符的值介于0到255之间。原子-1在文件结尾处返回。
让我们采用一个已创建的文件myfile.txt 。
#!/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
这产生以下结果-
File opend successfully
Read content : My name is Zara and age is 8
File closed successfully
Euphoria提供了许多方法的列表,这些方法可以帮助您处理文件。在Euphoria库例程中列出了这些方法。