Код IT
← Каталог

Работа со строками, таблицами и файлами — Пример 3 — Обработка больших файлов

Фрагмент из «Работа со строками, таблицами и файлами»: Пример 3 — Обработка больших файлов.

Lua main.lua
function process_large_file(input_file, output_file)
    local input = io.open(input_file, "r")
    local output = io.open(output_file, "w")
    
    if not input or not output then
        if input then input:close() end
        if output then output:close() end
        return false
    end
    
    for line in input:lines() do
        local processed = string.gsub(line, "%s+", " ")
        output:write(processed, "\n")
    end
    
    input:close()
    output:close()
    return true
end

process_large_file("input.log", "output.log")
function process_large_file(input_file, output_file)
    local input = io.open(input_file, "r")
    local output = io.open(output_file, "w")
    
    if not input or not output then
        if input then input:close() end
        if output then output:close() end
        return false
    end
    
    for line in input:lines() do
        local processed = string.gsub(line, "%s+", " ")
        output:write(processed, "\n")
    end
    
    input:close()
    output:close()
    return true
end

process_large_file("input.log", "output.log")