Help with filesystem?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
natev
Prole
Posts: 14
Joined: Thu Nov 20, 2014 3:55 pm

Help with filesystem?

Post by natev »

I know this is dumb, but...

How can I most simply create a text file if it doesn't exist, then append a line of text, then close it?

I'm trying to create simple output to a textfile (for debugging purposes, mostly). I would like to have something like,

Code: Select all

i, emergencyBrake = 0, 10^10
while table.continue do
     i = i +1
     ...
     if i==emergencyBrake then debug:log("logfile.txt", "Loop went on too long, table contains "..debug:dump(table)); break end
end
Lua io.write() isn't creating a new file (like I've read it is supposed to?). love.filesystem.newFile() isn't creating a file anywhere (although love.filesystem.createDirectory() is creating a directory in my user folder), and, in any case, it looks like the love filesystem is tuned for streams and such rather than what I want to do (data:new()?)

Any help would be greatly appreciated!
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Help with filesystem?

Post by s-ol »

love.filesystem.write automatically creates a file if needed:

Code: Select all

local str = "blergh blergh blergh"
if love.filesystem.isFile( "sometime.txt" ) then
 str = love.filesystem.read( "sometime.txt" ) .." \n".. str
end
love.filesystem.write( "somefile.txt",  str ) 

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
natev
Prole
Posts: 14
Joined: Thu Nov 20, 2014 3:55 pm

Re: Help with filesystem?

Post by natev »

Thank you very much, that's working great!
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Help with filesystem?

Post by Robin »

That is really bad, especially if you write often and if the file grows large.

Use [wiki]love.graphics.append[/wiki]("logfile.txt", "This is a line in the log") instead.
Help us help you: attach a .love.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Help with filesystem?

Post by kikito »

Robin wrote:That is really bad, especially if you write often and if the file grows large.

Use [wiki]love.graphics.append[/wiki]("logfile.txt", "This is a line in the log") instead.
Robin clearly meant filesystem instead of graphics there:

[wiki]love.filesystem.append[/wiki]("logfile.txt", "This is a line in the log")
When I write def I mean function.
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Help with filesystem?

Post by Azhukar »

kikito wrote:Robin clearly meant filesystem instead of graphics there
I must admit that was pretty funny.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Help with filesystem?

Post by Robin »

What. Kikito is right. That's one weird brain fart.
Help us help you: attach a .love.
User avatar
Doctory
Party member
Posts: 441
Joined: Fri Dec 27, 2013 4:53 pm

Re: Help with filesystem?

Post by Doctory »

Robin wrote:What. Kikito is right. That's one weird brain fart.
this happens way too often to me
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Help with filesystem?

Post by bartbes »

For some reason, every time I attribute a function to the wrong module, it's always to love.graphics. I guess it's just the module I type the name of most often.
User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Re: Help with filesystem?

Post by undef »

It's also a common typo for me to write love.graphics.
That, and loval instead of local...
twitter | steam | indieDB

Check out quadrant on Steam!
Post Reply

Who is online

Users browsing this forum: Majestic-12 [Bot] and 61 guests