Page 1 of 1

how use the love.filesystem

Posted: Tue Jul 31, 2018 5:47 am
by sky2046
I recently encountered some difficulties when i create a log file and directory.
why did i call the function[love.filesystem.createDirectory] failed, the following show code.

Code: Select all

logDir = "./log"
logPath = "./log/sad.log"

function love.load()
   loadStatus, errString = initLog()
end

function love.draw()
    if (false == loadStatus) then
        love.graphics.printf("progress load fail:\r\n \t" .. errString, 0, 0, 1024, "left")
    end
    
end

function love.update(dt)

end

function initLog()
    cwd = love.filesystem.getWorkingDirectory()
    if ("/" ~= string.sub(cwd, -1, -1)) then
        cwd = cwd .. "/"
    end
    
    if ("./" == string.sub(logDir, 1, 2)) then
        logDir = cwd .. string.sub(logDir, 3)
    end
  
    if (nil == love.filesystem.getInfo(logDir, "directory")) then
        if (false == love.filesystem.createDirectory(logDir)) then
            return false, "can not create directory[" .. logDir .. " ]"
        end
    end
    
    
    if ("./" == string.sub(logPath, 1, 2)) then
        logPath = cwd .. string.sub(logPath, 3)
    end

    if (nil == love.filesystem.getInfo(logPath, "file")) then
        logFile = love.filesystem.newFile(logPath)
        if (false == logFile:open("r")) then
        	return false, "can not create file[" .. logPath .. " ]"
        end
    end
end

Re: how use the love.filesystem

Posted: Tue Jul 31, 2018 12:51 pm
by grump
You can only write to the save directory. It is different from the working directory, and the only control you have over it is setting its name (not path/location) through the game's identity. This is all explained here.