Other Input
Euphoria can get input in a variety of ways, not just from the keyboard. Another common way of feeding data to a program is through a file. (You can also get input through the various ports on your computer, such as from joysticks, MIDI keyboards, or thermometers!) We're going to look at getting input from a file… those other input methods will be left for you to explore.
Before we get started, create a text file called "mydata.ini" and put your full name in it.
Now, enter the following code into a new file and call it "inputs.ex."
sequence name
atom fn
fn = open( "mydata.ini" , "r" )
name = gets( fn )
close( fn )
puts(1, "\n" & name & "\n" )
Save the file and then run it from your command line.
eui inputs.ex
If it ran properly, you should see your name appear on the screen and then be returned to the prompt. If it didn't run properly, make sure you specified the right filename to open and that the filename you opened has your name inside it.
Let's see what happened!
First, we define our two variables. The sequence name will hold the result of receiving the input from our file. The atom fn will hold a file identification number, or what is termed a "handle" to the file. I'll explain this in a bit.
Next, we use the open() function to open the file and return a handle (or identification number) to the file "mydata.ini." We want to open the file for reading, so we specify "r" as the second parameter.
Next, we use the function gets() to read a sequence from the opened file. Basically, we tell gets() to "get the next sequence" from the file identified by fn, which is "mydata.ini."
Now that we've got our data, we close the file with the close() procedure.
Finally, we print our data using puts().
In the following section "Output," we'll see how to output data to files. This is handy for when you want to save settings between running your programs or for sharing the data created by your program with others.