문제

I have a semi-complex question/help request for Lua coding. I'm a beginning, though I do have a basic understanding of Lua. Mostly looking in to IO (Which I hope is the correct thing to look into). Anyway, my question how would I go about reading a file, converting all the characters in the file into others (like the character "A" into the character "B", even if it's in a word) and then append it at the end of the file.

Current code (I know it's not much, but the each tutorial for IO input/output on files is vague and differs. Plus I generally started today....)

Lua 5.1

file = io.open("dump.txt", a+)
modifyable = file:read()

From what I understand, I'm reading from the file "dump.txt", and using Append+, which should add it at the bottom. And then I'm reading the file, so that I can add the changes needed. Am I wrong in any way and/or what do I need to do to achieve my goal?

I also read around and say that file:write would allow me to write to this line, but would that work since I'm using a+?

도움이 되었습니까?

해결책

You could do it with a+ but why have the file open while transforming:

file = io.open("dump.txt", 'r')
modifyable = file:read()
file:close()
modified = doModifications(modifyable) -- (like changing all A to B)
file = io.open("dump.txt", 'a')
file:write(modified)

If you want to use a+:

file = io.open("dump.txt", 'a+')
modifyable = file:read()
modified = doModifications(modifyable) -- (like changing all A to B)
file:write(modified)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top