Jump to content




Possible to automatically edit a program?


  • You cannot reply to this topic
2 replies to this topic

#1 Yalani

  • New Members
  • 1 posts

Posted 06 April 2014 - 10:23 AM

Oh hi,

I need a way to (not manually) edit existing programs on a floppy disk.
I know I can run "shell.edit(file)" in a program, but can I also tell the computer to actually change the code?

For example:
I have a disk with a program which stores a variable
var1 = 2
and a computer with a variable
var2 = 42

can i tell the computer to replace var1 in the disk with its own var2?

Greetings

#2 CometWolf

  • Members
  • 1,283 posts

Posted 06 April 2014 - 09:09 PM

This is where the Fs API comes in
http://computercraft...ki/Fs_%28API%29
You can use it to modify, read and create new files. In your case what you need would be something like
local file = fs.open("/path","w") --spcify file path and which mode to use, in this case "w" is the write mode
file.writeLine(var2) --write the variable var2 to the first line in the file, next time we use writeLine it will go on the second line.
file.close() --It's important to close the file handle once we're done with the file
Keep in mind however that to edit an existing file, you'll first need to copy it into the same file, as opening a file in write mode will clear it's contents.

#3 RoD

  • Members
  • 313 posts

Posted 06 April 2014 - 09:15 PM

Just adding a thing:
to read the file do:
local file = fs.open("/path", "r")
content = file.readAll()
file.close()
print(content)
The variable content now has the text that is inside the file





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users