1. Simple I/O Problem

Hello,

So I am working with some simple file I/O, however when I go to test it, nothing appears on the screen.

atom handle 
object line 
 
handle = open("myfile.txt","w") 
 
if handle = -1 then 
    puts(1,"Could not create file!") 
    else 
	print(handle,"hello") 
end if 
 
close(handle) 
 
handle = open("myfile.txt","r") 
 
if handle = -1 then 
    puts(1,"Could not open file!") 
    else 
	line = gets(handle) 
end if 
 
close(handle) 

Any help as to why nothing appears on the screen. I am missing a simple step, a bug? It should create the text file, then read the text file and output what was in the text file. I am using Euphoria 4.0.5 and Windows 7 64-bit Ultimate.

new topic     » topic index » view message » categorize

2. Re: Simple I/O Problem

Hi ice_viking If I'm understanding the question correctly, the answer is that you've read text into the object 'line', but you haven't printed it to the screen. PeterR

new topic     » goto parent     » topic index » view message » categorize

3. Re: Simple I/O Problem

Icy_Viking said...

Hello,

So I am working with some simple file I/O, however when I go to test it, nothing appears on the screen.

atom handle 
object line 
 
handle = open("myfile.txt","w") 
 
if handle = -1 then 
    puts(1,"Could not create file!") 
    else 
	print(handle,"hello") 
end if 
 
close(handle) 
 
handle = open("myfile.txt","r") 
 
if handle = -1 then 
    puts(1,"Could not open file!") 
    else 
	line = gets(handle) 
 
        --add this 
        puts(1, line) 
 
        --also should do more error checking on line in case read past end of file 
        --(this is instead of above) 
        if sequence(line) then 
           puts(1, line) 
        else 
           puts(1, "End of file reached\n") 
        end if 
 
        --also you should be looping to read subsequent lines, or use read_lines to read the whole file at once 
 
end if 
 
close(handle) 

Any help as to why nothing appears on the screen. I am missing a simple step, a bug? It should create the text file, then read the text file and output what was in the text file. I am using Euphoria 4.0.5 and Windows 7 64-bit Ultimate.

Cheers

Chris

new topic     » goto parent     » topic index » view message » categorize

4. Re: Simple I/O Problem

Thanks, it worked. Although I had to change it to printf instead of print, otherwise it printed out {100,110,1010,1013} Or something along those lines. I'll post the code to help others.

include get.e 
 
atom handle 
object line 
 
handle = open("myfile.txt","w") 
 
if handle = -1 then 
    puts(1,"Could not create file!") 
    else 
	printf(handle,"hello") 
end if 
 
close(handle) 
 
handle = open("myfile.txt","r") 
 
if handle = -1 then 
    puts(1,"Could not open file!") 
    else 
	line = gets(handle) 
	 
	puts(1,line) 
end if 
new topic     » goto parent     » topic index » view message » categorize

5. Re: Simple I/O Problem

You ought to use puts rather than printf. If you want to print formatted input then it is

printf(file_handle, format, arguments) 

So, if you are printing a simple string then you should use puts.

puts(file_handle, string) 

S D Pringle

new topic     » goto parent     » topic index » view message » categorize

6. 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: a gap of 30spaces; place Hello Euphoria right justified

( thanks Pete)

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

new topic     » goto parent     » topic index » view message » categorize

7. Re: Simple I/O Problem

_tom said...

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

Not quite. print does not take three parameters. printf(1,"30%s",{text}) would print "30Hello Euphoria" (without the quotes), and printf(1,"%30s",{text}" would print it padded to 30 characters, ie with 16 leading spaces.

Pete

new topic     » goto parent     » topic index » view message » categorize

8. Re: Simple I/O Problem

petelomax said...
_tom said...

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

Not quite. print does not take three parameters. printf(1,"30%s",{text}) would print "30Hello Euphoria" (without the quotes), and printf(1,"%30s",{text}" would print it padded to 30 characters, ie with 16 leading spaces.

Pete

Thanks!

Another good choice is ppp.e which is found in the Phix language ( http://phix.is-great.org/index.php ).

This is something we should add to the OE language.

_tom

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu