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

Disks

Started by grand_mind1, 19 January 2013 - 04:54 PM
grand_mind1 #1
Posted 19 January 2013 - 05:54 PM
I want to be able to put my disk into a computer that has information on it saying I have 100 dollars and when I do something on the computer it takes away a few dollars and the information on the card will change as well. I know how to do the money and the subtraction of money, but I don't even know where to begin with editing the disk. I thought that using fs.open in "w" mode would allow me to edit information on the disk, but either I don't get it or that's not what it is used for. Could someone kindly nudge me in the correct direction on how I would be able to do this?
Help is appreciated!
Thanks! :D/>

Also, sorry if I have bad English.
Edited on 19 January 2013 - 04:56 PM
theoriginalbit #2
Posted 19 January 2013 - 06:24 PM
Reading a file

local file = fs.open( filePath, "r" )
local contents = file:readAll()
file:close()

Writing to a file

local 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 :)/>
Edited on 19 January 2013 - 05:27 PM
grand_mind1 #3
Posted 19 January 2013 - 08:22 PM
Reading a file

local file = fs.open( filePath, "r" )
local contents = file:readAll()
file:close()

Writing to a file

local 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 :)/>
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()?
theoriginalbit #4
Posted 19 January 2013 - 08:27 PM
Apparently "w+" is an unsupported mode.
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 ) ;)/>
crazyguymgd #5
Posted 19 January 2013 - 11:20 PM
Apparently "w+" is an unsupported mode.
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 ) ;)/>
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.
theoriginalbit #6
Posted 19 January 2013 - 11:26 PM
Yeah w+ isn't supported in cc.
Hmmm could have sworn I used it… maybe not… I better go update the wiki then :P/>
crazyguymgd #7
Posted 19 January 2013 - 11:36 PM
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…
theoriginalbit #8
Posted 19 January 2013 - 11:43 PM
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…
I meant this page http://computercraft.info/wiki/IO_(API)
crazyguymgd #9
Posted 19 January 2013 - 11:47 PM
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…
I meant this page http://computercraft.info/wiki/IO_(API)
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.
theoriginalbit #10
Posted 19 January 2013 - 11:50 PM
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.
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…
crazyguymgd #11
Posted 19 January 2013 - 11:55 PM
well that's interesting because http://computercraft.info/wiki/Fs_(API) doesn't list fs.close() at all.
theoriginalbit #12
Posted 20 January 2013 - 12:21 AM
well that's interesting because http://computercraft.info/wiki/Fs_(API) doesn't list fs.close() at all.
Indeed… I'll fix that up when I get back on my computer too…
grand_mind1 #13
Posted 20 January 2013 - 07:22 AM
I think I have made some progress.
I have:

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?
mibac138 #14
Posted 20 January 2013 - 07:25 AM

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 ? ;)/>
grand_mind1 #15
Posted 20 January 2013 - 07:33 AM
While I do appreciate your help, this still doesn't seem to work.
This is what I have on test3

owner = "Alex"
And I have exactly what you wrote on my reading script.
mibac138 #16
Posted 20 January 2013 - 07:41 AM
New code:


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

If it not is what you want tell me what you want :P/>

@Edit: Edited code ;)/>
grand_mind1 #17
Posted 20 January 2013 - 07:57 AM
So basically what I am going to use this for is for my banking system for my server. People will be able to carry around their floppy disk (debit card) and use it with my store program. The only thing that will be on the card is the owner of the card so that no one can edit their money amount. So when someone puts their floppy disk into the store, that store will send messages to the central database to find out the password and amount of money that person has. So all I need the store to do now is find out who the owner of the card is. I am just super new to reading the files and manipulating them.
mibac138 #18
Posted 20 January 2013 - 08:14 AM
Oh, okey :P/>
So this is the new code (again)

Client

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

Server

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
grand_mind1 #19
Posted 20 January 2013 - 08:29 AM
Sorry, I already had a store program. :P/> All I need to know is how to get owner = "Alex" from a disk and into my main computer so that it can check for information on user: Alex.
mibac138 #20
Posted 20 January 2013 - 08:32 AM
Okey :P/>


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

I think so this is what you want ;)/>
grand_mind1 #21
Posted 20 January 2013 - 08:38 AM
Thank you so much. Your help was GREATLY appreciated. I've been trying to get this to work for a while now and have posted many topics on the same subject, but you are the first one to actually answer my question!
Thanks!! :D/>
mibac138 #22
Posted 20 January 2013 - 08:39 AM
Yay XD