1. Simple I/O Problem
- Posted by Icy_Viking May 14, 2016
- 1390 views
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.
2. Re: Simple I/O Problem
- Posted by peterR May 15, 2016
- 1379 views
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
3. Re: Simple I/O Problem
- Posted by ChrisB (moderator) May 15, 2016
- 1368 views
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
4. Re: Simple I/O Problem
- Posted by Icy_Viking May 15, 2016
- 1339 views
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
5. Re: Simple I/O Problem
- Posted by SDPringle May 16, 2016
- 1310 views
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
6. Re: Simple I/O Problem
- Posted by _tom (admin) May 16, 2016
- 1298 views
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.
( thanks Pete)
- the other choice is
display( text )
_tom
7. Re: Simple I/O Problem
- Posted by petelomax May 16, 2016
- 1282 views
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
8. Re: Simple I/O Problem
- Posted by _tom (admin) May 16, 2016
- 1302 views
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