Jump to content




Disks


21 replies to this topic

#1 grand_mind1

  • Members
  • 207 posts

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 by grand_mind1, 19 January 2013 - 05:56 PM.


#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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 by TheOriginalBIT, 19 January 2013 - 06:27 PM.


#3 grand_mind1

  • Members
  • 207 posts

Posted 19 January 2013 - 08:22 PM

View PostTheOriginalBIT, on 19 January 2013 - 06:24 PM, said:

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()?

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 19 January 2013 - 08:27 PM

View Postgrand_mind1, on 19 January 2013 - 08:22 PM, said:

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 ) ;)

#5 crazyguymgd

  • Members
  • 139 posts
  • LocationUSA

Posted 19 January 2013 - 11:20 PM

View PostTheOriginalBIT, on 19 January 2013 - 08:27 PM, said:

View Postgrand_mind1, on 19 January 2013 - 08:22 PM, said:

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.

#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 19 January 2013 - 11:26 PM

View Postcrazyguymgd, on 19 January 2013 - 11:20 PM, said:

Yeah w+ isn't supported in cc.
Hmmm could have sworn I used it... maybe not... I better go update the wiki then :P

#7 crazyguymgd

  • Members
  • 139 posts
  • LocationUSA

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...

#8 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 19 January 2013 - 11:43 PM

View Postcrazyguymgd, 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...
I meant this page http://computercraft.info/wiki/IO_(API)

#9 crazyguymgd

  • Members
  • 139 posts
  • LocationUSA

Posted 19 January 2013 - 11:47 PM

View PostTheOriginalBIT, on 19 January 2013 - 11:43 PM, said:

View Postcrazyguymgd, 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...
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.

#10 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 19 January 2013 - 11:50 PM

View Postcrazyguymgd, 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.
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...

#11 crazyguymgd

  • Members
  • 139 posts
  • LocationUSA

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.

#12 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 20 January 2013 - 12:21 AM

View Postcrazyguymgd, 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.
Indeed... I'll fix that up when I get back on my computer too...

#13 grand_mind1

  • Members
  • 207 posts

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?

#14 mibac138

  • Members
  • 132 posts
  • LocationPoland

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 ? ;)

#15 grand_mind1

  • Members
  • 207 posts

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.

#16 mibac138

  • Members
  • 132 posts
  • LocationPoland

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 ;)

#17 grand_mind1

  • Members
  • 207 posts

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.

#18 mibac138

  • Members
  • 132 posts
  • LocationPoland

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


#19 grand_mind1

  • Members
  • 207 posts

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.

#20 mibac138

  • Members
  • 132 posts
  • LocationPoland

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 ;)





3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users