Luaで良く作る関数 ~io編~

printf

-- いわゆるprintf。
function printf(...)
   local function wrapper(...) io.write(string.format(...)) end
   local status, result = pcall(wrapper, ...)
   if not status then error(result, 2) end
end

io.file_exists

-- 対象のfilenameのファイルが存在するかどうか。
-- 対象のファイルのフルパス。もしくは、カレントからの相対パス。
function io.file_exists(path)
    local fh = io.open(path, "rb")
    if fh then fh:close() end
    return fh ~= nil
end

io.file_size

-- 対象のfilenameのファイルのファイルサイズを得る
-- ファイルが存在しなければ、エラーとなる。
function io.file_size(filename)
    local fh = assert(io.open(filename, "rb"))
    local len = assert(fh:seek("end"))
    fh:close()
    return len
end

トップ   差分 履歴 リロード   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2016-03-18 (金) 00:00:00