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

How do I make a file read-only?

Started by CastleMan2000, 25 April 2012 - 09:13 PM
CastleMan2000 #1
Posted 25 April 2012 - 11:13 PM
I'm making a phonebook program, and I tried to use the fs.open readAll function, but it won't display anything, so I substituted it for a shell.run. Is there a way I can make this file read-only, but my program can still write to it? Like making it temporarily not read-only when the program needs it?
MysticT #2
Posted 25 April 2012 - 11:17 PM
No, the only way to make a file read-only is to put it in the rom folder, and it wouldn't be writable in-game.
CastleMan2000 #3
Posted 25 April 2012 - 11:19 PM
No, the only way to make a file read-only is to put it in the rom folder, and it wouldn't be writable in-game.

Hmmm. I see. Then is there a way to fix my problem with the fs.open? Because currently shell.run does the trick, but I would like to have it so you can only enter entries with a special function and not by editing the phonebook inside of the phonebook. EDIT: Oh great, now the writing feature doesn't work. That basically collapses the point of my program, although it didn't work in the first place. DOUBLE EDIT: Turns out the shell.run doesn't allow editing because it exits on the press of a key, so that's good. Now I need help with the writing part.
MysticT #4
Posted 25 April 2012 - 11:25 PM
Post your code (or at least the part with the problem) so we can help.
fs.open won't make the file read-only, so after your program writes to it, it can be edited by any other program (like edit).
CastleMan2000 #5
Posted 25 April 2012 - 11:30 PM

local h = fs.open("/phonebook.cfg", "w"
h.write("ID: ".. read() .." Name: " ..read())
These are the two lines that write to the file, and it won't work.

Post your code (or at least the part with the problem) so we can help.
fs.open won't make the file read-only, so after your program writes to it, it can be edited by any other program (like edit).

Also, I don't mind that.
OmegaVest #6
Posted 25 April 2012 - 11:31 PM
Hide it! If I remember correctly, and he hasn't taken it out/didn't put it in, putting a "." in front of a document will hide it. Of course, anyone who cracks the code will know it is there.
CastleMan2000 #7
Posted 25 April 2012 - 11:34 PM
Hide it! If I remember correctly, and he hasn't taken it out/didn't put it in, putting a "." in front of a document will hide it. Of course, anyone who cracks the code will know it is there.

That does make sense, I might do that. Thanks for the tip!
LipJ #8
Posted 25 April 2012 - 11:51 PM

local h = fs.open("/phonebook.cfg", "w"
h.write("ID: ".. read() .." Name: " ..read())
These are the two lines that write to the file, and it won't work.

You need to do

h.close()

And I think that possibly its

h.writeLine("ID: ".. read() .." Name: " ..read())
CastleMan2000 #9
Posted 26 April 2012 - 12:04 AM

local h = fs.open("/phonebook.cfg", "w"
h.write("ID: ".. read() .." Name: " ..read())
These are the two lines that write to the file, and it won't work.

You need to do

h.close()

And I think that possibly its

h.writeLine("ID: ".. read() .." Name: " ..read())

It still won't work. :)/>/> At least on the reading end maybe, I'm confused. I think I'll post all of the code in the Programs area, look for "RedPhonebook".
CastleMan2000 #10
Posted 26 April 2012 - 12:21 AM
Posted. Please go to the RedPhonebook thread in Program Library. This problem is really driving me crazy.
MysticT #11
Posted 26 April 2012 - 12:32 AM
I would recommend doing it like this:

write("Enter ID: ")
local id = read()
write("Enter Name: ")
local name = read()
local h = fs.open("/phonebook.cfg", "w")
h.writeLine("ID: "..id.." Name: "..name)
h.close()
But it should work anyway. Does it throw any error? Or it doesn't write to the file? Or something else maybe?
cant_delete_account #12
Posted 26 April 2012 - 01:30 AM
@CastleMan2000, you need to do it like:

function write(sID, sName)
local sFile = fs.open("//.phonebook.cfg","w")
sFile.writeLine("ID: "..sID.." Name: "..sName)
sFile.close()
end
term.clear()
term.setCursorPos(1,1)
write("ID: ")
local sID = tostring(read())
if sID then
term.clear()
term.setCursorPos(1,1)
else
error("Please enter something!")
end
write("Name: ")
local sName = tostring(read())
if sName then
term.clear()
term.setCursorPos(1,1)
else
error("Please enter something!")
end
write(sID, sName)
MysticT #13
Posted 26 April 2012 - 01:36 AM
@CastleMan2000, you need to do it like:

function write(sID, sName)
local sFile = fs.open("//.phonebook.cfg","w")
sFile.writeLine("ID: "..sID.." Name: "..sName)
sFile.close()
end
term.clear()
term.setCursorPos(1,1)
write("ID: ")
local sID = tostring(read())
if sID then
term.clear()
term.setCursorPos(1,1)
else
error("Please enter something!")
end
write("Name: ")
local sName = tostring(read())
if sName then
term.clear()
term.setCursorPos(1,1)
else
error("Please enter something!")
end
write(sID, sName)
I don't see the point of doing tostring(read()), since read() already returns a string, so tostring() would do nothing. Also, read() always returns a string value, never returns nil, so checking that is also pointless.
But I agree that it's better to use a function, that way you can use it in multiple parts of the program without repeating code (wich is what functions are for).
CastleMan2000 #14
Posted 26 April 2012 - 02:21 AM
@CastleMan2000, you need to do it like:

function write(sID, sName)
local sFile = fs.open("//.phonebook.cfg","w")
sFile.writeLine("ID: "..sID.." Name: "..sName)
sFile.close()
end
term.clear()
term.setCursorPos(1,1)
write("ID: ")
local sID = tostring(read())
if sID then
term.clear()
term.setCursorPos(1,1)
else
error("Please enter something!")
end
write("Name: ")
local sName = tostring(read())
if sName then
term.clear()
term.setCursorPos(1,1)
else
error("Please enter something!")
end
write(sID, sName)

Oh my gosh, it works! Thank you!!