lua-users home
lua-l archive

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


I'm trying to use io.lines() to read lines from a file which contains \0
characters. This seems to fail, probably because of the fgets() used in

Because - unlike C strings/char arrays - Lua strings are 8-bit safe, the
expected behaviour for me was for io.lines() to simply handle the \0's
as regular characters. 

  local fd =, "r+")
  fd:seek("set", 0)
  i = 0
  for l in fd:lines() do
     i = i + 1
     print(i, l)

I would expect the above to show

  1    one<\0>one<\n>
  2    two<\0>two<\n>
  3    three<\0>three<\n>

instead, it returns

  1    onetwothree<\n>

(control characters enclosed in <> by me)

I did not find any references in the documentation about how iolib is
supposed to handle zeros. Is the behaviour I see intented, or more of an
side effect of the usage of C-strings in the implementation of
io.lines() ?