打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
lua i/o read() 有时字符串连接异常
使用 f:read("*line") 读取字符,有时在拼接字符串时,会遇到后缀字符串覆盖前文的情况,根据分析应该lua读取的是个指针,所以附加字符串时会出现覆盖情况,

解决方式是 用 f:read("*all") 将文件一次性读取出来,然后分析字符串拆分

示例1:
function getFile(file_name)
  local f = assert(io.open(file_name, 'r'))
  local string = f:read("*line")
  f:close()
  return string
end

s = getFile("/home/faxfile/faxserver.ini")

print ("###/n"..s.."*****/n")
----------------------------
显示:
*****/nNS]

示例2:
function getFile(file_name)
  local f = assert(io.open(file_name, 'r'))
  local string = f:read("*all")
  f:close()
  return string
end

s = getFile("/home/faxfile/faxserver.ini")

print ("###/n"..s.."*****/n")
--------------------------------
显示:
###/n[DNS]
dns=trafax60:root:tomisoft
[Gateway]
callnumber=214
ip=192.168.0.81*****/n



测试代码如下
————————————————————————————————————

--[[
local function read_files( fileName )
c={}
local f = assert(io.open(fileName,'r'))
--local content = f:read("*all") 
for i in f:lines() do
table.insert(c,i)
print("lines:"..i)
end

f:close()

return c
end 

read_files("/home/faxfile/faxserver.ini")

-- 不是table  在调用pairs函数的时候跑出异常
-- bad argument #1 to 'pairs' (table expected, got number)

--存储外呼号码、文件及子表id
local function Set_config(rows)
local i = 0;
for key,val in pairs(rows) do
faxtask[i] = val;
if (bdebug == 1) then  --是否显示调试信息
print("faxtask"..i..": key->"..key.." val->"..val);
end
i = i+1;
end;
end;


local rlt = read_files("/home/faxfile/faxserver.ini")
local i = 0;
for key,val in pairs(rlt) do
print("faxtask"..i..": key->"..key.." val->"..val);
i = i+1;
end;

print(rlt)

--string.math("原字符串", "要找的字符串"),也是在字符串中搜索,但是注重的是模式匹配的那部分子字符串
--在字符串date中找出特定格式的信息,并输出。Example:


local date = "My birthday is 8/4/1991!"
local result = string.match(date, "%d+/%d+/%d+")
print(result)
local date = "db =1"
local result = string.match(date, "%=")
print(result)

]]

--[[
local function CallChannel( )

odbc = {}
--local f = assert(io.open("/home/faxfile/faxserver.ini",'r'))
-- odbc_reader.lua,读取odbc.ini文件并解析
--require "pl.pretty"
g=0


for line in io.lines('/home/faxfile/faxserver.ini') do
 temp = line:match('%[(.+)%]')
 if temp then
print("temp~~"..temp.."~~")
if temp ~= '' then
 source = temp
 odbc[source] = {}
 print("##"..source.."+##")
end
 elseif source and line ~= '' then
key, svalues = line:match('(%w+)%s*=%s*(.*)')
print(g)
print("source~~"..source.."~~")
print("key~~"..key.."~~")
print("svalues~~"..svalues.."~~")
odbc[source][key] = svalues
print("$$@@$$"..odbc[source][key])
g=g+1
 end

end
print("source~~"..source.."~~")
print("key~~"..key.."~~")
print("@@$$@@$$@@"..odbc[source][key])
end 

CallChannel()


print("[db]-usr"..odbc["gw"]["gwip"])
print(table.concat(odbc["gw"], "%"))
print(table.getn(odbc["gw"]))
i=0
for key,val in pairs(odbc) do
for key,vals in pairs(val) do
print("faxtask"..i..": key->"..key.." val->"..vals);
i = i+1;
end;
end;
---Set_config(rlt)

local function read_files( fileName )
c={}
local f = assert(io.open(fileName,'r'))
--local content = f:read("*all") 
for i in f:lines() do
table.insert(c,i)
print("lines:"..i)
end

f:close()

return c
end 
]]

local function read_files( fileName )
local filelines = {};
local i=0;
local g=0;
local f = assert(io.open(fileName,'r'));

if f then
for fline in f:lines() do
 temp = fline:match('%[(.+)%]');
 if temp then
print("temp~~"..temp.."~~")
if temp ~= '' then
 source = temp
 filelines[source] = {}
 print("source##"..source.."+##")
end
 elseif source and fline ~= '' then
key, svalues = fline:match('(%w+)%s*=%s*(.*)')
print(g)
print("source~~"..source.."~~")
print("key~~"..key.."~~")
print("svalues~~"..svalues.."~~")
filelines[source][key] = svalues
print("$$@@$$"..filelines[source][key])
g=g+1
 end
end

end
f:close();
return filelines
end 

--获取配置文件配置项读取ini文件并解析
local function Get_IniFileValue(fileName)  
local filelines = {};
local i=0;
local g=0;
local f = assert(io.open(fileName,'r'));

if f then
--for fline in f:lines() do
for fline in f:read("lines") do
 temp = fline:match('%[(.+)%]');
 if temp then
--print("temp~~"..temp.."~~")
if temp ~= '' then
 source = temp
 filelines[source] = {}
 --print("source##"..source.."+##")
end
 elseif source and fline ~= '' then
key, svalues = fline:match('(%w+)%s*=%s*(.*)')
filelines[source][key] = svalues
--[[
print(g)
print("source~~"..source.."~~")
print("key~~"..key.."~~")
print("svalues~~"..svalues.."~~")
g=g+1]]
 end
end
--[[
for key,val in pairs(filelines) do
for key,vals in pairs(val) do
print("faxtask"..i..": key->"..key.." val->"..vals);
i = i+1;
end;
end;]]
end;

f:close();
return filelines;
end;

function getFile(file_name)
  local f = assert(io.open(file_name, 'r'))
  local string = f:read("*all")
  f:close()
  return string
end

--s = getFile("/home/faxfile/faxserver.ini")

--print ("###/n"..s.."*****/n")

--获取配置文件配置项读取ini文件并解析
local function Get_IniFileValue2(fileName)  
local filelines = {};
local i=0;
local g=0;
local k=0;
local f = assert(io.open(fileName,'r'));

if f then
for fline in f:lines() do
--for fline in f:read(lines) do
 print (k..fline)  
 k=k+1
 if fline then
key, svalues = fline:match('(%w+)%s*=%s*(.*)')
if key then
filelines[key] = svalues
--[[]]
print(g)
print("key~~"..key.."~~")
print("svalues~~"..svalues.."~~")
g=g+1
end;
 end;
end;
--[[]]
for key,val in pairs(filelines) do
--for key,vals in pairs(val) do
if key and val then
print("faxtask"..i..": key->"..key.." val->"..val);
end;
i = i+1;
--end;
end;
end;

f:close();
return filelines;
end;


--获取配置文件配置项读取ini文件并解析
local function Get_IniFileValues(fileName)  
local filelines = {};
local i=0;
local f = assert(io.open(fileName,'r'));

if f then
flines = f:read("*all");
for key,value in string.gmatch(flines, '(%w+)=(%w+)') do
 print("i#"..i.."#"..key.."="..value.."*")
 filelines[key]=value
 i=i+1
end
for key,value in string.gmatch(flines, '(%w+)=(%w+%.%w+%.%w+%.%w+)') do
 print("i#"..i.."#"..key.."="..value.."*")
 filelines[key]=value
 i=i+1
end
--[[
for key,val in pairs(filelines) do
for key,vals in pairs(val) do
print("lines"..i..": key->"..key.." val->"..vals);
i = i+1;
end;
end;]]
end;

f:close();
return filelines;
end;


ta = Get_IniFileValue2("/home/faxfile/faxserver.ini");
--ta = Get_IniFileValues("/home/faxfile/faxserver.ini");
--ta = read_files("/home/faxfile/faxserver.ini");
i=0
for key,val in pairs(ta) do
--for key,vals in pairs(val) do
print("faxtask"..i..": key->"..key.." val->"..val);
i = i+1;
--end;
end;

print ("###"..ta["dns"].."*****")
print ("###"..ta["tip"].."*****")











本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Lua常见问题回答 Lua FAQ non
一起学Lua(第五篇-表的引用赋值)(转)
lua简单地异或加密文件
Lua中的变量与赋值方法
Java面试中经常问到的算法题 - - JavaEye技术网站
大规模的lua开发
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服