Jump to content




File new lines not working

help utility

  • You cannot reply to this topic
4 replies to this topic

#1 minecraftwarlock

  • Members
  • 34 posts

Posted 29 September 2014 - 10: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


#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 29 September 2014 - 10: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 )


#3 minecraftwarlock

  • Members
  • 34 posts

Posted 29 September 2014 - 11:01 PM

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.

#4 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 29 September 2014 - 11:11 PM

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...intstream-to-ou

#5 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 30 September 2014 - 04: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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users