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

Problem writing to a file

Started by birdini, 26 November 2013 - 06:26 PM
birdini #1
Posted 26 November 2013 - 07:26 PM
Hello all. I was trying to create a script that would write strings to a file. I wanted the program itself to be able to utilize arguments to streamline manual control. The program doesn't throw any errors at me, it just refuses to write any strings to the designated file:

the program itself would be called "writer", so I'd type this into the command line…
> writer a file "hello, world!"


local tArgs={...}

function write(path,mode,str)
str=tostring(str) --tostring() functions for redundancy
mode=tostring(mode)
path=tostring(path)
local file=fs.open(path,mode)
file.writeLine(str)
file.close()
end

write(tArgs[2],tArgs[1],tArgs[3])

I was hoping someone might tell me what I'm doing wrong.

Edit: I've updated the post to address Lyqyd's question
Edited on 03 December 2013 - 05:43 PM
Lyqyd #2
Posted 26 November 2013 - 07:54 PM
Please give us an example command that you are using to invoke the program.
Cutecurtain #3
Posted 27 November 2013 - 04:27 AM
Something like this might work better:

writeFile = function(path, mode, str)
  local path, mode, str = tostring(path), tostring(mode), tostring(str)

  local file = io.open(path, mode)
  file:write(str)
  file:close()
end
Edited on 27 November 2013 - 03:29 AM
theoriginalbit #4
Posted 27 November 2013 - 04:53 AM
Something like this might work better:

writeFile = function(path, mode, str)
  local path, mode, str = tostring(path), tostring(mode), tostring(str)

  local file = io.open(path, mode)
  file:write(str)
  file:close()
end
Functionally no different than OP's code, except that it makes use of the IO API, which is just an extension of the FS API anyway. I'd say that the problem will definitely be with how the program is being run, which is what Lyqyd has already asked about.
Bomb Bloke #5
Posted 27 November 2013 - 05:33 AM
By the by, it's not a good idea to declare a global function called "write" (or any function called "write", for that matter). It'll replace the regular one, likely causing some rather odd results until that computer reboots.