lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mon, Oct 20, 2008 at 9:28 AM, Jeff Wise <jwise@sealyrealty.com> wrote:
I have some Lua code like:

 output:write(name1, "\n")
 output:write(name2, "\n")
 output:write(acctno, "\n")

This code works just fine. I later process the resultant file with multiple
C programs, and I am having difficulty. All three writes are string values,
but the lack of a NULL character at the end seems to SOMETIMES cause grief.
I haven't figured out this C problem yet. Is it a good programming practice
to use the statement below instead of the above?

 output:write(name1, "\0\n")

The C programs sometimes correctly get the string lengths and other times do
not. I haven't discovered a pattern to this failure, but my C expertise is
about the same as my Lua level. For example, if "name1" were X'6162630A0D'
(as verified with a hex editor), should this instead be X'616263000A0D'?
(This is Win/XP). If you look at the Lua-produced file with Notepad or
SCiTE, it looks as one would expect. The specific problem occurs when
"name2" is a dummy value, which I have assigned as "^??^". The C error seems
to be that "name1" and "name2" are read as a single line instead of two
distinct lines.


I wouldn't write zeroes into your strings from the Lua code just to satisfy some C programs - if someone's reading only the Lua code, it's puzzling. I'd just write the strings plainly from Lua, then figure out how to tackle this problem from the C end.

Nothing's immediately springing to mind there, but if the standard library functions to read a line of text aren't catching your dummy values correctly, perhaps you could look for a dummy value that does workr?

Evan