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 ="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+?

Was it helpful?


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

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

If you want to use a+:

file ="dump.txt", 'a+')
modifyable = file:read()
modified = doModifications(modifyable) -- (like changing all A to B)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top