Reading a byte value

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
modsyn
Posts: 28
Joined: Tue Sep 27, 2005 6:02 am

Reading a byte value

Post by modsyn »

Hi, I'm trying to read a value from a file and treat that value as the
number of characters to read next. I'm probably overlooking something
so simple, but I can't seem to figure out what it is. Here is my code

Code: Select all

    sfile  = io.open(infile,"r")
    numchar = sfile:read(1)
    dataout = sfile:read(tonumber(numchar))
and the error message I get is "bad argument #1 to 'read' (invalid option)"

the first value is (for my testing purposes) a byte value of 9 followed by
9 characters that represent a file name.

I'm stuck until I can figure this out, so any help is greatly appreciated.

EDIT*
It was something obvious and I got it fixed.

Code: Select all

    sfile  = io.open(infile,"r")
    numchar = string.byte(sfile:read(1))
    dataout = sfile:read(numchar)
Sorry to have mucked up the forum unnecessarily.
KawaGeo
Posts: 191
Joined: Sat Aug 27, 2005 6:52 am
Location: Calif Mountains

Post by KawaGeo »

Which read() that generated the error in your code?

Suggest to add print statement(s) for debugging purpose. Eg: print(numchar) after it is been read from the file. This should lead you to the finding.

Happy hunting. :)
Geo Massar
Retired Engineer
modsyn
Posts: 28
Joined: Tue Sep 27, 2005 6:02 am

Post by modsyn »

it was the first code. i had many debug prints, but i left them out for ease
of reading. i finally got it fixed, as i edited the original post to say.

i finally found a decent source of general lua documentation. from
lua.org (ha, go figure).
Post Reply