[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua and C strings
- From: Eike Decker <eike@...>
- Date: Mon, 20 Oct 2008 17:22:05 +0200
Probably a shot into the blue: If you open a file handle without a binary flag,
weird things like these can happen. Try to open the file with the "wb" or "rb"
flag instead of just "w" or "r". Otherwise it stops reading/writing when 0 byte
is found and whatever not else (does it change \n to \r\n?).
Eike
> 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.
>
>