找回密码
 立即注册
搜索
查看: 114|回复: 7

[已处理] LUA有返回文本行数到变量的方法吗?

  [复制链接]
发表于 2026-5-13 17:30:11 | 显示全部楼层 |阅读模式
建议反馈
建议描述: 如下
实现具体思路: 如下
联系QQ: 隐藏内容

功能:
    返回文本行数到变量
格式:
    格式:GetTxtRowsToVar 文件名 变量(S、N、A、G、个人临时变量) 是否绝对路径(0-相对路径[版本内]/1-绝对路径)
#IF
#ACT
GetTxtRowsToVar ..\QuestDiary\数据文件\杀怪奖励[排序].txt S5
#Say
S5:<$STR(S5)>\


LUA有这样的方法吗?如果没有,能增加一个吗?


回复

使用道具 举报

发表于 2026-5-13 22:18:35 | 显示全部楼层
function GetTxtRowsToVar(path)
    local file = io.open(path, "r")
    if not file then
        return 0
    end

    local n = 0
    for _ in file:lines() do
        n = n + 1
    end
    file:close()

    return n
end

print("文件行数:", GetTxtRowsToVar("D:/test2.txt"))
回复

使用道具 举报

发表于 2026-5-13 22:19:00 | 显示全部楼层
function GetTxtRowsToVar(path)
    local file = io.open(path, "r")
    if not file then
        return 0
    end

    local n = 0
    for _ in file:lines() do
        n = n + 1
    end
    file:close()

    return n
end

print("文件行数:", GetTxtRowsToVar("D:/test2.txt"))
回复

使用道具 举报

 楼主| 发表于 2026-5-14 21:44:36 | 显示全部楼层
鬼雨 发表于 2026-5-13 22:19
function GetTxtRowsToVar(path)
    local file = io.open(path, "r")
    if not file then

收到,谢谢,这个支持相对路径吗?
回复

使用道具 举报

发表于 4 天前 | 显示全部楼层
--=========================================================
-- 获取本地文本文件的行数
-- @param filePath 文件的绝对路径或相对路径
-- @return         返回整数行数 (如果文件不存在则返回 0)
--=========================================================
function GetFileLineCount(filePath)
    -- 以只读模式 ("r") 打开文件
    local file = io.open(filePath, "r")
   
    -- 安全校验:如果文件不存在或被占用,file 会是 nil
    if not file then
        print("读取失败,找不到文件或文件被占用: " .. tostring(filePath))
        return 0
    end

    local count = 0
    -- file:lines() 每次只在内存里读一行,极度节省内存
    for _ in file:lines() do
        count = count + 1
    end

    -- 必须关闭文件,释放句柄
    file:close()

    return count
end
回复

使用道具 举报

发表于 4 天前 | 显示全部楼层
深澜 发表于 2026-5-14 21:44
收到,谢谢,这个支持相对路径吗?

需要自己组装路径了
回复

使用道具 举报

 楼主| 发表于 4 天前 | 显示全部楼层
1580 发表于 2026-5-21 10:55
需要自己组装路径了

用上了。
回复

使用道具 举报

发表于 3 天前 | 显示全部楼层
已收集,因LUA的方式性能不是太高,还是由引擎实现个方法提供使用吧
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|HGEM2论坛

GMT+8, 2026-5-25 09:58 , Processed in 0.300215 second(s), 5 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表