lua-users home
lua-l archive

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


That's the point£¬ thank you very much.

rwen

---Original---
From: "Jonathan Goble"<jcgoble3@gmail.com>
Date: 2017/3/7 17:45:55
To: "Lua mailing list"<lua-l@lists.lua.org>;
Subject: Re: io.read seems not right on windws, why?

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.