Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua file:read unexpected behavior

I'm a complete newbie in Lua and i've stumbled upon a problem i don't understand.

So what i'm trying to do is open a file, read the data and save it into a different file with a different name.

Here is the code

local infile = io.open(folder..'/'..f, "r")
local instr = infile:read("*all")
infile:close()

local outfile = io.open(folder..'/'..newName, "w")
outfile:write(instr)
outfile:close()

The result i get is a Source file 288Kb and a Dest file 2Kb

So again, as i'm a newbie in Lua, the fact that the problem is in infile:read is a wild guess for me, but the way i see it, it's either infile:read or outfile:write.

UPD: The content is absolutely arbitrary, which implies special symbols occur.

Thank you in advance,

Regards!

like image 992
Aleksandr Kurinnoi Avatar asked Jun 28 '26 06:06

Aleksandr Kurinnoi


1 Answers

I've made it work by opening the in- and output file in binary mode by adding flag b in the io.open call., so the code I have now is

        local infile = io.open(folder..'/'..f, "rb")
        local instr = infile:read("*all")
        Log(instr)
        infile:close()

        local outfile = io.open(folder..'/'..newName, "wb")
        outfile:write(instr)
        outfile:close()
like image 194
Aleksandr Kurinnoi Avatar answered Jun 30 '26 01:06

Aleksandr Kurinnoi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!