forum-msg-id-129689-edit

Original date:2016-05-16 14:15:40 Edited by: _tom Subject: Re: Simple I/O Problem

Simple IO

One nice thing about Euphoria is that the include files are written in Euphoria. That means that you can look at the source-code and learn something. For example look at read_lines from std/io.e:

 
public function read_lines(object file) 
	object fn, ret, y 
	if sequence(file) then 
		if length(file) = 0 then 
			fn = 0 
		else 
			fn = open(file, "r") 
		end if 
	else 
		fn = file 
	end if 
	if fn < 0 then return -1 end if 
 
	ret = {} 
	while sequence(y) with entry do 
		if y[$] = '\n' then 
			y = y[1..$-1] 
			ifdef UNIX then 
				if length(y) then 
					if y[$] = '\r' then 
						y = y[1..$-1] 
					end if 
				end if 
			end ifdef 
		end if 
		ret = append(ret, y) 
		if fn = 0 then 
			puts(2, '\n') 
		end if 
	entry 
		y = gets(fn) 
	end while 
 
	if sequence(file) and length(file) != 0 then 
		close(fn) 
	end if 
 
	return ret 
end function 

Use this directly or as a starting point for your own, custom, input routine.


puts vs printf vs display

Lets start with:

sequence text = "Hello Euphoria"

  • Now, puts(1, text) is the traditional way of displaying a sequence as text: Hello Euphoria.
  • However, printf(1, text ) also has the same output: Hello Euphoria.

The intention of printf is to use a format string like:

printf(1, "%s", { text } ) which gives you the same result: Hello Euphoria

( The trick here is that you must write {text} to get the string else you get one character.)

Finally, printf makes more sense because you can add spacing:

print(1, "%30s", {text} ) which has the result: 30spaces followed by ... Hello Euphoria

And you can add more stuff:

atom ver = 4.1

printf(1, "%s %g", { text, ver } ) which has the result: Hello Euphoria 4.1

  • the other choice is

include std/console.e
display( text )

which also adds a few tricks of its own

_tom

Not Categorized, Please Help

Search



Quick Links

User menu

Not signed in.

Misc Menu