Thanks for the help!
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
[Question] program edit file
Started by alonikomax, 06 June 2012 - 04:17 AMPosted 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!
Thanks for the help!
Posted 06 June 2012 - 10:48 AM
Try the filesystem API, specifically fs.open()
Posted 06 June 2012 - 04:35 PM
Can you show me an example?Try the filesystem API, specifically fs.open()
Posted 06 June 2012 - 05:31 PM
The linked documentation on the seems pretty clear. What are you having trouble with?
Posted 06 June 2012 - 07:50 PM
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.The linked documentation on the seems pretty clear. What are you having trouble with?
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.
Posted 07 June 2012 - 12:17 PM
you can give me an exaple plz?Use f.readLine and f.writeLine along with tables, the latter which are in the lua reference document.
Posted 07 June 2012 - 01:17 PM
If you really want an example, here is one:
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.
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.
Posted 07 June 2012 - 04:19 PM
Thank you!!!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.
Done login system!
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..
Posted 20 June 2012 - 07:39 AM
That's because append mode won't give you read methods.
Posted 21 June 2012 - 03:52 AM
what is append mode?
Posted 21 June 2012 - 02:59 PM
Append mode keeps what has been written and then you edit…
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.