Cannot create file.

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
iPoisonxL
Party member
Posts: 227
Joined: Wed Feb 06, 2013 3:53 am
Location: Australia
Contact:

Cannot create file.

Post by iPoisonxL »

Hey there. Bad problem here. I'm working on a little thing and it's supposed to create a file. But it doesn't create a file. At all. It's supposed to create/update a file when you type "export whatever" into the console. The commands work fine, I've tested the crap out of them, but I just can't create a file. For debugging purposes, I made it that on mousepress it will create a file called "file.txt", but it's not working. I took the exact same code that I put in the Lua Interactive thing, which worked perfectly, but it just doesn't work with love2d.

I really need help with this, it's always happened with Love, and I have no idea why.
Also, if you're going to tell me to "not use lua's io functions", why not? And I've already tried love.filesystem, and it doesn't work either.

P.S. I'm working on a USB. No, it's not because of that. I've tested it on my desktop. Didn't work. The Lua Interactive works fine on my USB.
Attachments
notworking.love
(988 Bytes) Downloaded 165 times

Code: Select all

      L
    L Ö
    Ö V
L Ö V E
Ö B E
V E
E Y
Website
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Cannot create file.

Post by Boolsheet »

Works for me.

Did you check if io.open or file:write return error messages? It's common in Lua that the second return value is an error message when the first return value is nil.

Do you know the concept of the working directory? That's where the file will end up. On Windows for example, the shortcuts can define where this working directory is when they launch an application. If it is in the Program Files directory, then you most likely don't have access and get a permission denied error.

This is kind of why we don't recommend the Lua IO library. It provides a very basic way to read and write files, but so much more is needed to make it work properly on multiple platforms. Everyone has their own way of doing things and you have to know and follow these guidelines.
Shallow indentations.
User avatar
iPoisonxL
Party member
Posts: 227
Joined: Wed Feb 06, 2013 3:53 am
Location: Australia
Contact:

Re: Cannot create file.

Post by iPoisonxL »

Boolsheet wrote:Works for me.

Did you check if io.open or file:write return error messages? It's common in Lua that the second return value is an error message when the first return value is nil.

Do you know the concept of the working directory? That's where the file will end up. On Windows for example, the shortcuts can define where this working directory is when they launch an application. If it is in the Program Files directory, then you most likely don't have access and get a permission denied error.

This is kind of why we don't recommend the Lua IO library. It provides a very basic way to read and write files, but so much more is needed to make it work properly on multiple platforms. Everyone has their own way of doing things and you have to know and follow these guidelines.
There's no error, but I was testing with Notepad++'s run feature, maybe that's why? And where would my working directory be? I always assumed it was in the same directory as the lua files.

EDIT: I believe it's because of Notepad++. Is there any way I can fix this?

Code: Select all

      L
    L Ö
    Ö V
L Ö V E
Ö B E
V E
E Y
Website
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Cannot create file.

Post by Boolsheet »

Probably in the Notepad++ directory then.

The working directory can be anywhere. LÖVE provides you this fabulous function [wiki]love.filesystem.getWorkingDirectory[/wiki] to find out where it is. Note that it will most likely return an absolute path and in platform-specific notation.

Oh, and for a "fix". You, or in this case Notepad++, passes the path to the game directory/archive as an argument to love.exe, right? Check the arguments in the table passed to love.load or the global arg table. An absolute path will get you directly to it and a relative path can be used together with the working directory path.
Shallow indentations.
User avatar
iPoisonxL
Party member
Posts: 227
Joined: Wed Feb 06, 2013 3:53 am
Location: Australia
Contact:

Re: Cannot create file.

Post by iPoisonxL »

Boolsheet wrote:Probably in the Notepad++ directory then.

The working directory can be anywhere. LÖVE provides you this fabulous function [wiki]love.filesystem.getWorkingDirectory[/wiki] to find out where it is. Note that it will most likely return an absolute path and in platform-specific notation.

Oh, and for a "fix". You, or in this case Notepad++, passes the path to the game directory/archive as an argument to love.exe, right? Check the arguments in the table passed to love.load or the global arg table. An absolute path will get you directly to it and a relative path can be used together with the working directory path.
I don't understand what you mean.

The arguments like these ones? "c:\your\path\to\love.exe" "$(CURRENT_DIRECTORY)" is what I use to run Love.

Code: Select all

      L
    L Ö
    Ö V
L Ö V E
Ö B E
V E
E Y
Website
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Cannot create file.

Post by Boolsheet »

Yep. You can now check in love.load or the global arg table what $(CURRENT_DIRECTORY) expanded into.

Code: Select all

function love.load(args)
	local path_to_game = args[1]
end
This will be the path and file name of a love archive if you start it from that.
Shallow indentations.
User avatar
iPoisonxL
Party member
Posts: 227
Joined: Wed Feb 06, 2013 3:53 am
Location: Australia
Contact:

Re: Cannot create file.

Post by iPoisonxL »

Boolsheet wrote:Yep. You can now check in love.load or the global arg table what $(CURRENT_DIRECTORY) expanded into.

Code: Select all

function love.load(args)
	local path_to_game = args[1]
end
This will be the path and file name of a love archive if you start it from that.
So what am I supposed to do with this here? :/

Sorry, I don't quite understand.

EDIT: I made this to see what would happen. Path to main file is on my USB, then doesn't that mean I should have the files be created on that said USB? Because it creates them in the Notepad++ directory (dozens of files just.. there... lol)

Code: Select all

      L
    L Ö
    Ö V
L Ö V E
Ö B E
V E
E Y
Website
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Cannot create file.

Post by Boolsheet »

iPoisonxL wrote:EDIT: I made this to see what would happen. Path to main file is on my USB, then doesn't that mean I should have the files be created on that said USB? Because it creates them in the Notepad++ directory (dozens of files just.. there... lol)
The Lua IO is relative to the working directory if you don't give it absolute paths. We have to show it the way to the game directory. We can do that with the argument that is passed to LÖVE.

Code: Select all

function love.load(args)
	local path = args[1]
	if not path then
		text = "No arguments passed to love?"
		return
	end
	
	-- Making sure we have a slash at the end.
	path = path:gsub("([^\\/])$", "%1/")

	local file, err = io.open(path .. "file_beside_main_lua.txt", "wb")
	if not file then
		error("We encountered an error when opening the file: " .. tostring(err))
	end

	file:write("hello\r\n")
	file:write("Working directory was: " .. love.filesystem.getWorkingDirectory() .. "\r\n")
	file:write("First argument was: " .. args[1] .. "\r\n")
	file:close()

	-- Reading it back for confirmation.
	file = io.open(path .. "file_beside_main_lua.txt", "rb")
	text = file:read("*a")
	file:close()

	-- Default font doesn't like CR. :(
	text = text:gsub("\r", "")
end

function love.draw()
	love.graphics.print(text, 10, 10)
end
Shallow indentations.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 156 guests