Shane Gadsby
Shane Gadsby

Reputation: 1298

Issues getting all data from an image file using Lua io.read('*a')

I'm trying to get all the data from image file (jpg/jpeg/gif/png/bmp etc.) use Lua's io.read() function, but I'm not having much luck as it only seems to read a small piece of the data.

As a side note all plain text files are being read just fine, so I'm assuming that the problem is with character encoding or some such thing.

Example:

local data
local fileHandle

fileHandle = io.open ( 'pic.jpg')
data = fileHandle:read('*a')

print(data)

Upvotes: 2

Views: 3124

Answers (1)

Alexander Gladysh
Alexander Gladysh

Reputation: 41403

If you're on Windows, open file as binary: io.open('pic.jpg', 'rb').

Also it is a good idea to wrap io.open() in assert() to catch errors (or do handle them otherwise, of course).

Upvotes: 7

Related Questions