Page 1 of 1

File I/O with bytes

Posted: Sun Jan 31, 2016 2:44 pm
by FroggestSpirit
I'm a little stuck with having code create, or load an existing file, and write to it.

Code: Select all

function love.load(arg)
	love.filesystem.setIdentity("fileTest")
	str = string.char(72,101,108,108,111,10) -- "Hello\n"
	love.filesystem.write("test.bin",str)
	love.window.close()
end
That's my code right now, but it does nothing to test.bin which already exists

Re: File I/O with bytes

Posted: Sun Jan 31, 2016 2:58 pm
by zorg
Hi!
Could you try this instead, and report back on the results?:

Code: Select all

function love.load(arg)
   love.filesystem.setIdentity("fileTest")
   str = string.char(72,101,108,108,111,10) -- "Hello\n"
   local result = love.filesystem.write("test.bin",str)
   print(result)
   love.window.close()
end

Re: File I/O with bytes

Posted: Sun Jan 31, 2016 4:29 pm
by FroggestSpirit
@zorg it ended up not doing anything. I moved the print command to a love.draw function, and remove the window closing, but it still came up with nothing displayed

Re: File I/O with bytes

Posted: Sun Jan 31, 2016 5:07 pm
by Xugro
Your Code works on my machine. Have you looked in the right place (see: https://love2d.org/wiki/love.filesystem)? The file test.bin is not saved where the .love file is. You have to look in the save directory of love.

Re: File I/O with bytes

Posted: Sun Jan 31, 2016 5:23 pm
by FroggestSpirit
Xugro wrote:Your Code works on my machine. Have you looked in the right place (see: https://love2d.org/wiki/love.filesystem)? The file test.bin is not saved where the .love file is. You have to look in the save directory of love.
Thank you, I found it in the path specified. Is there a way I can change it to use the path of the source or .love file?

Re: File I/O with bytes

Posted: Sun Jan 31, 2016 7:59 pm
by zorg
Through the FFI, you can do that, but it has some issues, mainly that you can't guarantee the OS gives your program the rights to actually edit/modify files in that folder, or that the folder you want to be writable won't be the one the below code sets, and that if you have files opened elsewhere, changing the writable directory will fail.
With that said, something like this might work.
I'd only ever use this for some personal projects of mine, where i know what the damage this might be able to cause :3

Re: File I/O with bytes

Posted: Mon Feb 01, 2016 4:15 am
by FroggestSpirit
Thank you for the replies! If I end up doing a music editing program, I'll probably use it as a fused-exe, because from what I understand, you can have it read/write files in the same directory