how can i use io.open to open a unicode path in lua

Viewed 902

here is my code:

local path = "C:/Users/KayWang/Desktop/测试.txt"
local file,err = io.open(path,"rb")
print(file,err)

and this is the output:

nil C:/Users/KayWang/Desktop/测试.txt: No such file or directory

I don't want to use winapi and I really want to know why this function cann't support unicode path?

1 Answers

In general it is not possible.

E.g. my Windows system has default cp-1251 (Russian) so I can not represent your path in it. So to be able open this file I have to use unicode path. But C api does not provide such functionality. But if your code page support your path then you can convert your unicode path to it and use it with io.open. In other case you have to use some C code. I think easy way is just use MS extension like _wfopen wich uses UTF-16 as arguments. Other way is use CreateFileW and then wrap handle to file descriptor and this descriptor to file pointer.

Also you can use some external IO library which supports such path. I just test my libuv binding and be able read/write file on my system. (File path in utf-8)

Related