lua向文件中寫入數據,進行記錄



function
readfile(path) local file = io.open(path, "r") if file then local content = file:read("*a") io.close(file) return content end return nil end function writefile(path, content, mode) mode = mode or "w+b" local file = io.open(path, mode) if file then if file:write(content) == nil then return false end io.close(file) return true else return false end end --文件讀寫測試 local a = {x=1,y=2,z=3} local str = json.encode(a) --將lua表編碼為json格式字符串 local path = cc.FileUtils:getInstance():getWritablePath() path = path..filename --得到可讀寫文件的路徑 writefile(path,str,r) --將數據寫入文件 local b = readfile(path) --從文件中讀取數據 printf(b) end


2. 文件操作示例代碼
local path = cc.FileUtils:getInstance():getWritablePath()
cc.FileUtils:getInstance():writeToFile({key="value"}, paht.."filename") --讀取數據的方式
print(ret.key)
lcoal ret = cc.FileUtils:getInstance():getValueMapFromFile(Path.."filename") --讀取數據的方式2
print(str)
local str = cc.FileUtils:getInstance():getStringFromFile(path.."filename")




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM