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

File System API Bug

Started by Ninjawolf0007, 07 December 2013 - 12:02 AM
Ninjawolf0007 #1
Posted 07 December 2013 - 01:02 AM
>receive
bios:339: [string "receive"]:12: ')' expected

Spoiler

local modem = peripheral.wrap("left")
modem.open(1)

local message = os.pullEvent("modem_message")

print("Up 1 confirmed!")


z = fs.open("disk/startup", "w")
z.write(x=x+1)
z.close()
os.shutdown()

I don't see anything wrong with this code, could someone please help me out? I'm getting frustrated with this because it doesn't make sense. From what I can tell, the syntax is all right and there are no errors….

Also a pastebin if you want to see the code there: http://pastebin.com/853NHtiF
theoriginalbit #2
Posted 07 December 2013 - 01:07 AM
You cannot do this

z.write(x=x+1)
did you mean for it to be a string like so?

z.write("x=x+1")
Ninjawolf0007 #3
Posted 07 December 2013 - 01:13 AM
You cannot do this

z.write(x=x+1)
did you mean for it to be a string like so?

z.write("x=x+1")
Probably… I want to change a variable inside of a file. Do I need to type the math for it as a string?
Lyqyd #4
Posted 07 December 2013 - 01:16 AM
All that will do is write the literal string to the file, so the contents of the file after you run this code will be:


x=x+1

If you want to change something that's already in the file, you'll have to open the file in read mode, read it into a variable (or a table of lines), change the specific spot you wish to increment the value of, then write the whole file contents to disk again.
Ninjawolf0007 #5
Posted 07 December 2013 - 01:18 AM
All that will do is write the literal string to the file, so the contents of the file after you run this code will be:


x=x+1

If you want to change something that's already in the file, you'll have to open the file in read mode, read it into a variable (or a table of lines), change the specific spot you wish to increment the value of, then write the whole file contents to disk again.

How would I go about that though? Sorry for not knowing a lot, I just started looking into the more complicated part of computercraft.
Bomb Bloke #6
Posted 07 December 2013 - 08:39 AM
That depends on what's already in the file (or "may" be in the file), and what that file's going to be used for. I'm guessing you want to write a series of commands to a floppy disk, which a turtle will boot from later? In that case, just putting numbers there won't cut it. You'd be better off building a table in memory with details as to which movements are to be made and in what order, then writing the whole file in one go once you've got all the data you need to write out the full list of commands.
Edited on 07 December 2013 - 07:39 AM
Ninjawolf0007 #7
Posted 07 December 2013 - 10:51 AM
Here's my setup in a test world:
SpoilerFront:



Top:

Far left is Computer 1, then on top of the disk drive is Computer 2, then next right, down 1 is the Turtle receiving the startup command that's on the disk. Then the turtle that deploys the other turtles and turns them on. The chest contains fuel and that's it.

Code:
SpoilerComputer 1:
Spoiler

local modem = peripheral.wrap("top")

while true do
	 local event = os.pullEvent("redstpne")
	 print("Input received!")		 --For debugging only, no other real purpose.
	 modem.transmit(1,1, "Up 1")
end

Computer 2:
Spoiler

local modem = peripheral.wrap("left")
mode.open(1)

local message = os.pullEvent("modem_message")

print("Up 1 confirmed")	 -- Debugging purposes only


-- [[ Commented out because this is the issue where I don't know what to do.

z = fs.open("disk/startup", "r")
z.readAll()
z.close
os.shutdown()

]]--

Disk
Spoiler

x = 2

turtle.turnLeft()
turtle.select(1)
turtle.suck()
turtle.refuel()
turtle.turnLeft()
turtle.turnLeft()
turtle.up()

for i=1, x do
turtle.forward()
end

turtle.digDown()
rs.setOutput("left", true)
turtle.down()

Far Right Turtle:
Spoiler

print(peripheral.getType("front"))   --Purely debugging once again.
P = peripheral.wrap("front")
P.turnOn()

-snip-

You'd be better off building a table in memory with details as to which movements are to be made and in what order, then writing the whole file in one go once you've got all the data you need to write out the full list of commands.
How would I do this? And build it into the memory? I honestly have no clue how to do this… But thank you for your help.
Edited on 07 December 2013 - 11:26 AM
Ninjawolf0007 #8
Posted 07 December 2013 - 02:14 PM
Can someone please help me out?


Ok, so here's what I have, nothing has changed except for the disk drive startup:

Spoiler

local tab = {2, 1}

os.setComputerLabel("WorldExcav".. tab[2])

turtle.turnLeft()
turtle.select(1)
turtle.suck()
turtle.refuel()
turtle.turnLeft()
turtle.turnLeft()
turtle.up()

for i=1, tab[1] do
turtle.forward()
end

turtle.digDown()
rs.setOutput("left", true)
turtle.down()

How do I edit the table "tab" and nothing else from a different program?

I'm at the tables API: http://computercraft.info/wiki/Tables, but it says how to save tables to a seperate file, but then how do I edit the copied table and insert in as the original?
Edited on 07 December 2013 - 02:36 PM
Bomb Bloke #9
Posted 07 December 2013 - 05:21 PM
Since ComputerCraft doesn't allow you to alter an existing part of a file (short of wiping the whole thing and re-writing it all), I would say your best bet here is to have two files on the disk - the "startup" script the turtle is to boot from, and an additional "data" file. Have Computer 2 read/write a number - or a table full of numbers - to the "data" file, and have the "startup" script on the disk read your number (… or table full of numbers) from that same "data" file.

Note that when I say "write a number", I mean that you'd just do something like "z.write(x)" (which'd put the contents of "x" in the file). If you wanted to retrieve that number back into your "x" variable, then you'd open the file in read mode and do "x = tonumber(z.readLine())".
Ninjawolf0007 #10
Posted 07 December 2013 - 05:34 PM
Since ComputerCraft doesn't allow you to alter an existing part of a file (short of wiping the whole thing and re-writing it all), I would say your best bet here is to have two files on the disk - the "startup" script the turtle is to boot from, and an additional "data" file. Have Computer 2 read/write a number - or a table full of numbers - to the "data" file, and have the "startup" script on the disk read your number (… or table full of numbers) from that same "data" file.

Note that when I say "write a number", I mean that you'd just do something like "z.write(x)" (which'd put the contents of "x" in the file). If you wanted to retrieve that number back into your "x" variable, then you'd open the file in read mode and do "x = tonumber(z.readLine())".

Thank you!! I've been trying to figure this out for 2+ hours trying to do the complicated process of converting tables back and forth from table to string…
This helped a lot! Thank you.