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

[Question] program edit file

Started by alonikomax, 06 June 2012 - 04:17 AM
alonikomax #1
Posted 06 June 2012 - 06:17 AM
I want to make auto add programs to my OS, how I make a program to edit another program or file?
Thanks for the help!
my_hat_stinks #2
Posted 06 June 2012 - 10:48 AM
Try the filesystem API, specifically fs.open()
alonikomax #3
Posted 06 June 2012 - 04:35 PM
Try the filesystem API, specifically fs.open()
Can you show me an example?
kazagistar #4
Posted 06 June 2012 - 05:31 PM
The linked documentation on the seems pretty clear. What are you having trouble with?
alonikomax #5
Posted 06 June 2012 - 07:50 PM
The linked documentation on the seems pretty clear. What are you having trouble with?
I need to make login system with more than 1 user, i need the login program to edit the users file and add more users into it.
OmegaVest #6
Posted 06 June 2012 - 08:01 PM
Use f.readLine and f.writeLine along with tables, the latter which are in the lua reference document.
alonikomax #7
Posted 07 June 2012 - 12:17 PM
Use f.readLine and f.writeLine along with tables, the latter which are in the lua reference document.
you can give me an exaple plz?
Orwell #8
Posted 07 June 2012 - 01:17 PM
If you really want an example, here is one:


filename = 'users'
userfile = fs.open(filename, 'a')	-- 'a' stands for append mode, you also have 'w' (write) and 'r' (read)
while true do
  line = userfile.readLine()	  -- read a line to the variable 'line'
  if not line then		-- there are no more lines
	break
  end
  print(line)		  -- print the line you just read
end
userfile.writeLine('new user')	-- append a line to the file
userfile.close()		  -- close the file (or could you've guessed it? :)/>/>)

Note: code is untested

But really, a word of advice, you can do yourself a huge favor by learning Lua from the absolute beginning (statements, functions, conditionals, loops, …).
Maybe you understand these all ready, but when you learn further from there one, you automatically gain the knowledge and insight to understand the documentation on the wiki or from other sources.
It might seem like a waste of time, and you want to get going, but it really is a must to write decent programs.
alonikomax #9
Posted 07 June 2012 - 04:19 PM
If you really want an example, here is one:


filename = 'users'
userfile = fs.open(filename, 'a')	-- 'a' stands for append mode, you also have 'w' (write) and 'r' (read)
while true do
  line = userfile.readLine()	  -- read a line to the variable 'line'
  if not line then		-- there are no more lines
	break
  end
  print(line)		  -- print the line you just read
end
userfile.writeLine('new user')	-- append a line to the file
userfile.close()		  -- close the file (or could you've guessed it? :)/>/>)

Note: code is untested

But really, a word of advice, you can do yourself a huge favor by learning Lua from the absolute beginning (statements, functions, conditionals, loops, …).
Maybe you understand these all ready, but when you learn further from there one, you automatically gain the knowledge and insight to understand the documentation on the wiki or from other sources.
It might seem like a waste of time, and you want to get going, but it really is a must to write decent programs.
Thank you!!!
Done login system!
Mitzey234 #10
Posted 20 June 2012 - 04:38 AM
If you really want an example, here is one:


filename = 'users'
userfile = fs.open(filename, 'a')	-- 'a' stands for append mode, you also have 'w' (write) and 'r' (read)
while true do
  line = userfile.readLine()	  -- read a line to the variable 'line'
  if not line then		-- there are no more lines
	break
  end
  print(line)		  -- print the line you just read
end
userfile.writeLine('new user')	-- append a line to the file
userfile.close()		  -- close the file (or could you've guessed it? :(/>/>)

Note: code is untested

But really, a word of advice, you can do yourself a huge favor by learning Lua from the absolute beginning (statements, functions, conditionals, loops, …).
Maybe you understand these all ready, but when you learn further from there one, you automatically gain the knowledge and insight to understand the documentation on the wiki or from other sources.
It might seem like a waste of time, and you want to get going, but it really is a must to write decent programs.

that won't work at all for me..
Lyqyd #11
Posted 20 June 2012 - 07:39 AM
That's because append mode won't give you read methods.
ETHANATOR360 #12
Posted 21 June 2012 - 03:52 AM
what is append mode?
tfoote #13
Posted 21 June 2012 - 02:59 PM
Append mode keeps what has been written and then you edit…
Lyqyd #14
Posted 21 June 2012 - 03:22 PM
When opening a file with the FS API, specifying the "a" mode will cause all file writes to appear at the end of the file, after the existing contents (they are "appended"). Normal write mode overwrites the existing file.