[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: RE: opening a file in binary mode
- From: "Honglei Tu" <honglei@...>
- Date: Tue, 20 Mar 2001 17:42:42 -0800
Do you know how to call Lua functions recursively.
Such as I have a Lua function
local numEntries, e, name, flag, value
numEntries = GetNumEntriesForTable(t)
print("number of entries is ", numEntries)
for i=1, numEntries do
e = GetTableEntryByIndex(b, i)
name = GetEntryName(e)
flag = GetEntryFlag(e)
value = GetEntryValue(e)
print("name is ", name)
print("flag is ", flag)
print("value is ", value)
if (flag == 32)
But I failed to recursively call gettable in gettable.
Anyone can tell me how to call lua functions recursively ?
[mailto:email@example.com]On Behalf Of Luiz Henrique de
Sent: Tuesday, March 20, 2001 5:00 PM
To: Multiple recipients of list
Subject: Re: opening a file in binary mode
>In any case, how do I open a file in binary and write binary data to it? (I
don''t want my numbers converted to strings, as that's what seems to be
You open the file with openfile(name,"wb"), as you already know ("w+b" if
want to append instead of overwriting).
To write binary data, you could in principle use strchar(byte) as Pedro
suggested, but that assumes that you know how to break your binary data into
bytes. For integers, it's simple with a sequence of divisions by 256 (or
you could use "%08X" in format and then write strchar(byte) of each 2 hex
digits). For numbers with fractional part, it's harder, but you may get by
by using frexp. You might prefer to write a simple library that does this
you. Something like this (untested!):
static tobinary(lua_State* L)
if arg is number then
else if arg is string
; /* do nothing */
and export this to Lua. Then you'd simply do write(tobinary(x)) in Lua.