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

Editing a certain line in code

Started by Exerro, 22 June 2012 - 10:56 AM
Exerro #1
Posted 22 June 2012 - 12:56 PM
Im trying to make an edit function so you type in the program you want to edit, the line you want to edit, and what you want the line to say but i can't figure out how…what i have so far is:


programtoedit = read()
fileopen = fs.open(programtoedit, "r")
what-it-says = fileopen.readAll()
fileopen.close()
print(what-it-says)
linetoedit = read()
whattowrite = read()
file-editer = fs.open(programtoedit, "a")
file-editer.writeLine(whattowrite, linetoedit)
file-editer.close()

I really need some help so could you suggest what i could do instead
OmegaVest #2
Posted 22 June 2012 - 02:34 PM
Few things, code-tagged and commented.


programtoedit = read()
fileopen = fs.open(programtoedit, "r")   --I notice you never check whether or not the file exists. Might do that or it will simply run without any of it, leaving you wondering what went wrong
what-it-says = fileopen.readAll()
fileopen.close()
print(what-it-says)
linetoedit = read()	 -- You will need a tonumber around this read.
whattowrite = read()
file-editer = fs.open(programtoedit, "a")	 -- Append will only tack on to the end. Open it in Write.
file-editer.writeLine(whattowrite, linetoedit)	--This is not how writeLine works. writeLine just puts in \n at the end for you instead of you needing to do so.
file-editer.close()

The easier method of doing this is to delete the file after you have it, modify the line, replace the line in the table, then when finished, write the whole thing back to a similar place.
So, something akin to this:


progEdit = io.read()
editProg = {}
if fs.Exists(progEdit) then
  fileIn = fs.Open(progEdit, r)[/font][/color]
   editProg = fileIn[/font][/color].readAll()
   fileIn.close()
else
   print("No such file")
   return
end

--I'm leaving out the print here because that could be a problem for programs longer than 18 lines.

while true do

   term.clear()
   term.setCursorPos(1,1)

   term.write("Line to edit:  ")
   lineEd = tonumber(io.read())
   print(" ")
   print(editProg[lineEd])
   print(" ")
   break
end	--This while loop for asking the user if the line is correct.
print("What is the new line to read?")
writeWhat = io.read()

editProg[lineEd] = writeWhat


fileOut = fs.open(progEdit, w)  --Now, I'm not sure how to make it start at line 1, but I think just overlaying the table on top of it works. Mystic or somebody will probably correct me.
for x=1, #editProg do
   fileOut.writeLine(editProg[x])
end
fileOut.close()

Now, this is only simple code. Eventually, this would have to have a way to modify line in real time, instead of a response based program. But, that is something I don't think anyone can do on the fly. Also, for future reference, the forum has what they call code tags. It's the little button with "<" and ">" on it. Makes life easier on everyone involved.
Exerro #3
Posted 22 June 2012 - 04:07 PM
Ok thanks ill try it out now :P/>/>
MysticT #4
Posted 22 June 2012 - 04:13 PM
The above code has some errors (mostly typos). This should work:

-- get the filename
write("Filename: ")
local filename = read()
-- open the file to read
local file = fs.open(filename, "r")
-- check if the file is open
if file then
  -- table to store lines
  local tLines = {}
  -- get a line from the file
  local sLine = file.readLine()
  while sLine do
    -- store it in the table
	table.insert(tLines, sLine)
    -- get the next line
	sLine = file.readLine()
  end
  -- close the file
  file.close()
  -- get the line number
  write("Line Number: ")
  local nLine = tonumber(read())
  -- check if it's a number
  if nLine then
    -- get the text to write
	write("New Text: ")
	local s = read()
    -- check if the line number is greater than the file lines
	if nLine > #tLines then
      -- add some empty lines
	  for i = #tLines, nLine - 1 do
		tLines[i] = ""
	  end
	end
    -- set the line to the new text
	tLines[nLine] = s
    -- open the file to write
	local file = fs.open(filename, "w")
    -- check if the file is open
	if file then
      -- write every line
	  for _,line in ipairs(tLines) do
		file.writeLine(line)
	  end
      file.close()
	else
	  print("Error opening file to save")
	end
  else
	print("Line has to be a number")
  end
else
  print("No such file ", filename)
end
It's a little bit messy, but it shouldn't be to hard to follow.
Exerro #5
Posted 22 June 2012 - 04:42 PM
that works perfect thanks guys