Writing to file - why won't this code work?
Monkey Forums/Monkey Programming/Writing to file - why won't this code work?
| ||
| This code appears to work, but no file actually gets created on disk. The problem seems to lie with using monkey://data/ ... if I hard-code the path, it works fine. I'm running it as a desktop app in OSX 10.9.3. Thanks, Lindsay
Strict
Import brl.filestream
Function WriteTestFile:Int(fname: String)
Local textFile: FileStream
' This line causes the app to fail
Local filename: String = "monkey://data/"+fname+".txt"
' But this line works just fine
' Local filename: String = "/Users/lindsay/Monkey/Learning/FileIOTest/FileIOTest.data/"+fname+".txt"
Print "Attempting to create: "+filename
textFile = New FileStream(filename,"w")
If textFile = Null
Print "Can't create text file"
Return 0
End
textFile.WriteString("Bananas!")
textFile.Close()
Return 1 ' Success
End
Function Main:Int()
If WriteTestFile("test") = 0
Print "Failure"
Else
Print "Success"
End
Return 0
End
Produces this output ... Attempting to create: monkey://data/test.txt Success |
| ||
Works fine here, the file is created within the resulting .app/Users/danilo/Projects/Monkey/filetest.buildv79d/glfw/xcode/build/Release/MonkeyGame.app/Contents/Resources/data/test.txt Right-click on your .app and select something like "show package content" to see it, or use ForkLift 2 file manager. |
| ||
| Oh, of course, I was expecting it to create the file in my *source* directory. So obvious now you explain it to me. Thanks! :) |