lua-users home
lua-l archive

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


On Tue, Mar 7, 2017 at 4:38 AM R.wen <rwen2012@qq.com> wrote:
hi,
   I just test the simple code as follow,

local f = io.open("lua53.dll")
print(f:seek("end"))
print(f:seek("set"))
local str = f:read("a")
print(#str)

it is output is:

230400
0
201

it only read out 201 bytes, but the file size is 230400, what is wrong here?

Windows distinguishes between text and binary files, so you need to explicitly open the file as a binary file, using io.open("lua53.dll", "rb") where the "b" in the mode specifies binary. If you don't specify binary mode, then text mode is assumed, and Lua (actually, the Windows C runtime, since Lua just delegates to that) cuts off f:read("a") at the first byte that represents an end-of-file control code.