Disks
grand_mind1 19 Jan 2013
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!
Also, sorry if I have bad English.
Edited by grand_mind1, 19 January 2013 - 05:56 PM.
Help is appreciated!
Thanks!
Also, sorry if I have bad English.
Edited by grand_mind1, 19 January 2013 - 05:56 PM.
theoriginalbit 19 Jan 2013
Reading a file
Writing to a file
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 by TheOriginalBIT, 19 January 2013 - 06:27 PM.
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 by TheOriginalBIT, 19 January 2013 - 06:27 PM.
grand_mind1 19 Jan 2013
TheOriginalBIT, on 19 January 2013 - 06:24 PM, said:
Reading a file
Writing to a file
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
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
theoriginalbit 19 Jan 2013
grand_mind1, on 19 January 2013 - 08:22 PM, said:
Apparently "w+" is an unsupported mode.
crazyguymgd 19 Jan 2013
TheOriginalBIT, on 19 January 2013 - 08:27 PM, said:
grand_mind1, on 19 January 2013 - 08:22 PM, said:
Apparently "w+" is an unsupported mode.
theoriginalbit 19 Jan 2013
crazyguymgd 19 Jan 2013
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 19 Jan 2013
crazyguymgd, on 19 January 2013 - 11:36 PM, said:
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...
crazyguymgd 19 Jan 2013
TheOriginalBIT, on 19 January 2013 - 11:43 PM, said:
crazyguymgd, on 19 January 2013 - 11:36 PM, said:
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 19 Jan 2013
crazyguymgd, on 19 January 2013 - 11:47 PM, said:
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.
crazyguymgd 19 Jan 2013
well that's interesting because http://computercraft.info/wiki/Fs_(API) doesn't list fs.close() at all.
theoriginalbit 20 Jan 2013
crazyguymgd, on 19 January 2013 - 11:55 PM, said:
well that's interesting because http://computercraft.info/wiki/Fs_(API) doesn't list fs.close() at all.
grand_mind1 20 Jan 2013
I think I have made some progress.
I have:
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 20 Jan 2013
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!") endThis you want ?
grand_mind1 20 Jan 2013
While I do appreciate your help, this still doesn't seem to work.
This is what I have on test3
This is what I have on test3
owner = "Alex"And I have exactly what you wrote on my reading script.
mibac138 20 Jan 2013
New code:
If it not is what you want tell me what you want
@Edit: Edited 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
@Edit: Edited code
grand_mind1 20 Jan 2013
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 20 Jan 2013
Oh, okey
So this is the new code (again)
Client
Server
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 20 Jan 2013
Sorry, I already had a store program. 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 Jan 2013
Okey
I think so this is what you want
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