This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
minecraftwarlock's profile picture

File new lines not working

Started by minecraftwarlock, 29 September 2014 - 09:32 PM
minecraftwarlock #1
Posted 29 September 2014 - 11:32 PM
I've been trying to create a program that writes a table of strings to a file, but I can't figure out how to make it start a new line.
Here is the code:

function new(data,page)
f = io.open(page,"w")
for _,line in ipairs(data) do
  f:write(line,"\n")
end
f:close(page)
end
Here is the line that calls the code:

new({"Hello World!,"Lua is cool"},"test")
Here is the file:

Hello World!Lua is cool
KingofGamesYami #2
Posted 29 September 2014 - 11:45 PM
I've never used io, I stick to fs, however I'm not sure why you are writing multiple times anyway.

local formatted = table.concat( data, "\n" )
f:write( formatted )
minecraftwarlock #3
Posted 30 September 2014 - 12:01 AM
I think notepad uses a different new line format because when I open the file with notepad it's all on one line, but when I open it with notepad++ or wordpad the lines are separate.
KingofGamesYami #4
Posted 30 September 2014 - 12:11 AM
Yes, notepad uses a return ( "\r" ) for it's new lines. Use "\r\n" if you want to fix that.

Article (about java, but same problem): http://stackoverflow.com/questions/5557978/notepad-not-recognizing-new-line-characters-in-java-code-using-printstream-to-ou
Bomb Bloke #5
Posted 30 September 2014 - 05:07 AM
If you use fs.open() to get your file handles, they'll have access to a writeLine() function (in addition to write()) which automatically handles the line breaks for you.