readbyte()

Blitz3D Forums/Blitz3D Beginners Area/readbyte()

El Neil(Posted 2006) [#1]
ok heres the deal.

im writing a typing tutor game where people can paste their own text into a file from wherever to use for practice. The game reads the letters one by one from the file, but i need to skip all spaces, punctuation, carriage returns etc.

here is the code i have made so far:



Function readletter()


If Eof("YOUR WORDS HERE.txt") Then SeekFile("YOUR WORDS HERE.txt",1)

;basically, if eof, then loop the file


Repeat
e = ReadByte("YOUR WORDS HERE.txt")
Until e > 64 And e <91
; cycle through the file until the number returned from readbyte() is within ascii code for A-Z
Return e

End Function



now, when i run this it bombs out rather spectacularly. im guessing that the number returned by readbyte(0 isnt the ascii value. can someone please advise me as to what the integer returned by this function represents, and how i could convert it to the relevant ascii code?


thank you
Neil


El Neil(Posted 2006) [#2]
GRRRRR!!!!! I keep posting this in blitzPlus forums but it appears in blitz3D every time. i am not doing this on purpose!

neil


BlackJumper(Posted 2006) [#3]
You probably need to obtain a valid filestream handle using OpenFile() or ReadFile() before you can read data out of it. I don't think you can directly use ReadByte on a named file...

so...
Function ReadLetter()
targetfile = ReadFile("YOUR WORDS HERE.txt")
if targetfile <> 0 then ; is the file handle valid ??
    Repeat
         e = ReadByte(targetfile)
    Until e > 64 And e <91
else
     e = -1  ; this indicates an error in opening the file
endif

Return e 
End Function


You will also have to add more code for the case where ReadByte encounters EoF during the loop. Blitz will read past the file end, but will return zero (probably forever) if your file does not end with a valid ASCII character.


Sir Gak(Posted 2006) [#4]
Blackjumper, I would amend your suggested code as follows:
Function ReadLetter()
my_e$=""
targetfile = ReadFile("YOUR WORDS HERE.txt")
if targetfile <> 0 then ; is the file handle valid ??
    while not eof(targetfile)
        e = ReadByte(targetfile)
        if e > 64 And e <91
          ;do something with the byte here, ie, my_e$=my_e$+chr$(e)
        endif
    wend
endif
;if my_e$ is NULL (ie empty) then nothing was read.
;what you do with this is up to you.
Return my_e$ 
End Function



jfk EO-11110(Posted 2006) [#5]
NOt sure if you can SeekFile after EOF was detected. You also could read the file size first, then use a counter when reading bytes, then Seekfile when you reached the max index, before you actually hit EOF.


El Neil(Posted 2006) [#6]
cheers guys. ill have a go.

neil


Sir Gak(Posted 2006) [#7]
Limbopper, as usual. ;o