Lua need to read the file, which I just wrote in same program -
need write file, open reading , write lines file - in 1 script. problem is, i:
open file1 in read mode (file1=io.open("my_file.txt","r"))
open file2 in write mode (file2=io.open("my_changed_file.txt","w"))
write changed content file1 file2
open file2 (tried open file3=io.open("my_changed_file.txt","r")) in read mode , print lines example
i tried several ways, file2:flush(), or file2:close() , re-open after finished writing, returns nil when want print lines
file1=io.open("my_file.txt","r") file2=io.open("my_changed_file.txt","w") line in file1:lines() file2:write(line.."changes") end file2:flush() file3=io.open("my_changed_file.txt","r") --write several lines file or --(need combine changed lanes file2 , original lines file1 based on key)
i've tried script minor changes in lua 5.1, 5.2, , 5.3 , works expected in versions. script below , there 1 change may important: write
doesn't add new line print
does, may need add if want output on different lines:
local file1=io.open("my_file.txt","r") local file2=io.open("my_changed_file.txt","w") line in file1:lines() file2:write(line.."changes\n") end file2:close() local file3=io.open("my_changed_file.txt","r") print(file3) line in file3:lines() print(line) end
Comments
Post a Comment