This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
cmdpwnd's profile picture

Can't write to file - unknown reason

Started by cmdpwnd, 06 March 2016 - 11:46 PM
cmdpwnd #1
Posted 07 March 2016 - 12:46 AM
I'm not a retard, why isn't this working?



ANSWER

--Lua console localizes variables to the line of the prompt. Exclude keyword 'local'
file = fs.open('filename','w')
file.write('line')
file.close()
Edited on 07 March 2016 - 01:29 AM
Bomb Bloke #2
Posted 07 March 2016 - 12:50 AM
fs.open() is returning nil. This means that either the file "filename" is already locked open by another process (rebooting your real computer should sort that out), or the ComputerCraft computer's hard drive is full (removing files should sort that, though rarely you'll need to restart the Minecraft server as well - check what fs.getFreeSpace("") tells you).
cmdpwnd #3
Posted 07 March 2016 - 12:55 AM
rebooted in game computer, and physical computer. Same error

fs.getFreeSpace('disk')
124000
Edited on 06 March 2016 - 11:56 PM
Bomb Bloke #4
Posted 07 March 2016 - 01:17 AM
Your screenshot doesn't show you attempting to write to "disk"…
cmdpwnd #5
Posted 07 March 2016 - 01:36 AM
Lyqyd #6
Posted 07 March 2016 - 01:37 AM
fs.open uses absolute paths.
cmdpwnd #7
Posted 07 March 2016 - 01:38 AM
so begin at '/' ?

/disk/file

or begin at 'disk' ?

disk/file
Lyqyd #8
Posted 07 March 2016 - 01:39 AM
In ComputerCraft, the leading slash is optional. Functions that require absolute paths will see /disk/file and disk/file as the same.
cmdpwnd #9
Posted 07 March 2016 - 01:41 AM
In ComputerCraft, the leading slash is optional. Functions that require absolute paths will see /disk/file and disk/file as the same.
tried both
Edited on 07 March 2016 - 12:43 AM
Bomb Bloke #10
Posted 07 March 2016 - 01:52 AM
Hah, you're localising "file". The Lua console localises to the line, so you discard the handle the moment you create it.
cmdpwnd #11
Posted 07 March 2016 - 01:57 AM
so, when i'm doing this in code i need to also keep it a non-localized variable? seems like a bug to me
Bomb Bloke #12
Posted 07 March 2016 - 02:17 AM
No, that's just a quirk of the Lua console - every command you put in gets its own scope. If you run that same code within a dedicated script file, then that ceases to be a problem.
Edited on 07 March 2016 - 01:18 AM
cmdpwnd #13
Posted 07 March 2016 - 02:23 AM
ok, gotcha. Thanks :)/>