Search code examples
filelualine-breaksmultiline

Writing multiline text files in Lua


I would like to know the best way to make my script write something into a file (lets say text.txt) in a way that would always add a line break at the end. When I append text using

file = io.open("test.txt", "a")
file:write("hello")

twice, the file looks like:

hellohello

But I want it to look like:

hello
hello

Solution

  • Unlike print, the new line character isn't added automatically when calling io.write, you can add it yourself:

    file:write("hello", "\n")