Help is appreciated!
Thanks! :D/>
Also, sorry if I have bad English.
local file = fs.open( filePath, "r" )
local contents = file:readAll()
file:close()
local file = fs.open( filePath, "w+" )
file:write( data )
file:close()
Apparently "w+" is an unsupported mode. Also, here would I put what I want to change? Like if I have money = 100 on my floppy disk and I want it to be changed to money = 20, would I put 'money = 20' between file:write(data) and file:close()?Reading a filelocal file = fs.open( filePath, "r" ) local contents = file:readAll() file:close()
Writing to a filelocal file = fs.open( filePath, "w+" ) file:write( data ) file:close()
the reason I use "w+" here instead of "w" is that with "w" it writes over the file when it opens, meaning you have no data in there, using "w+" only writes over the file when the file is being flushed (closed)… so if you had a server closure (SSP is classed as a server too) while the program is in the middle of writing the file, while using "w", BAM thats all the information gone… with "w+" its all still there…
http://lua-users.org...LibraryTutorial
http://www.lua.org/pil/21.html ( chapter 21 goes over multiple pages )
EDIT: Also its better if you keep your posts together instead of posting a new topic each time :)/>
Odd I could have sworn I used it the other day… Just use "w" then and try to keep the file open for as little time as possible ( meaning get a string ready with the data your going to write, then open, write and close ) ;)/>Apparently "w+" is an unsupported mode.
Yeah w+ isn't supported in cc. But like TheOriginalBIT said here, store the data that is already in the file, then write whatever information from the original file and what you want to add as quickly as possible so you minimize the probability of losing data due to two computers/turtles accessing the same file at the same time.Odd I could have sworn I used it the other day… Just use "w" then and try to keep the file open for as little time as possible ( meaning get a string ready with the data your going to write, then open, write and close ) ;)/>Apparently "w+" is an unsupported mode.
Hmmm could have sworn I used it… maybe not… I better go update the wiki then :P/>Yeah w+ isn't supported in cc.
I meant this page http://computercraft.info/wiki/IO_(API)The wiki states, in the fs.open page that the supported modes are "w", "r", "a", and "b" so there would be no need to update the wiki…
Ahhh, well that page definitely needs to have some info (given that there is none on that page), but the wiki page about the fs api is pretty good.I meant this page http://computercraft.info/wiki/IO_(API)The wiki states, in the fs.open page that the supported modes are "w", "r", "a", and "b" so there would be no need to update the wiki…
Yeh I might just grab the FS page and dump it in with the appropriate changes for the IO… OMG I'm gunna need to get rid of all the duplication in the FS page too… they have fs.close about 4 times… lol…Ahhh, well that page definitely needs to have some info (given that there is none on that page), but the wiki page about the fs api is pretty good.
Indeed… I'll fix that up when I get back on my computer too…well that's interesting because http://computercraft.info/wiki/Fs_(API) doesn't list fs.close() at all.
local file = fs.open("test3","r")
local contents = file:readAll()
print(contents)
like you showed me, I just added the 'print' part. Although I still don't know how to do things like check variables inside the file like I wanted it to. If I have owner = "me" on a disk and I want to check who the owner is using the code above, how would I go about doing this?
local file = fs.open("test3","r")
local contents = file:readAll()
print(contents)
if contents == "owner" then print("Access granted!") -- you can edit "owner"
else print("Wrong owner name!") end
This you want ? ;)/>
owner = "Alex"
And I have exactly what you wrote on my reading script.
local file = fs.open("test3","r")
local contents = file:readAll()
print(contents)
if contents == "owner = \"Alex\"" then
print("Access granted!")
else
print("Wrong owner name!")
print("Shutting down computer in 3 seconds...")
sleep(3)
os.shutdown()
end
serverId = --add here server id for example 61
local file = fs.open("card.info","r")
if not fs.exists("card.info") then
print("Error: Card: file card.info not exists")
end
local contents = file:readAll()
rednet.send(serverId, contents)
sleep(5)
id, message = rednet.recive()
if id == nil and message == nil then print("Server not responding. Please contact with administrator!")
elseif id == serverId and message == nil then print("You aren't in the database of the server!")
elseif id == serverId then
user, balance = message
print("Welcome "..user.."!")
print("Your account balance: "..balance)
end
Clients = {"X","X", "X"} -- replace X to the id of the clients computers
while true do
id, message = rednet.recive()
if id == Clients then -- i don't know what to do now :X
local serverId = --server id
local ci = fs.open("card.info","r")
local contents = file:readAll()
print(contents)
if contents == "owner = \"Alex\"" then
rednet.send(serverId, "Alex")
end