开发者

Lua Input Problem

开发者 https://www.devze.com 2023-03-04 05:08 出处:网络
I\'m new to the Lua I/O, but have been using Lua elsewhere for almost two years now. I have this line:

I'm new to the Lua I/O, but have been using Lua elsewhere for almost two years now. I have this line:

for line in io.lines("myfile.txt") do

but it always results in a "No such file or directory" error. "myfile.txt" is in the same directory as the Lua file.

Where should I put the file "myfile.txt"开发者_如何学编程?


for line in io.lines(assert(io.open("myfile.txt"))) do

-- The lines function accepts a file in its argument, while on the other hand, open accepts a string and returns the file.


I had same problem but fixed it with the following code - This code demonstrates iteration with io.open ...

function getHostnameFromHostFile(ip)
    local hostName = "unknown";
    local hostFile = io.open("/etc/hosts");

    for line in hostFile:lines() do
        if line and string.find(line, ip) then
            hostName = line:match("[a-zA-Z][a-zA-Z0-9]+");
        end
    end
    return hostName;
end
0

精彩评论

暂无评论...
验证码 换一张
取 消