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

Reading external files

Started by gabriel2anjos, 17 May 2012 - 06:30 PM
gabriel2anjos #1
Posted 17 May 2012 - 08:30 PM
Hi people! Actually, i'm working in a currency system called MineCard. It works based in accounts. I know how to read the external files using the fs thing. I did some coding, but i didn't understand the write and read thing soooooo good. So, im going need some help… I also dont know what is the os.pullEventRaw() thing. Explain, please!

Byeeee.
Dirkus7 #2
Posted 17 May 2012 - 09:35 PM
To open the file, use

yourfile = fs.open("filename", 'r') -- r for read, w for write
Then use

filecontent = yourfile.readAll()
filecontent is a string with all the text in the file
If you have writemode (fs.open('filename', 'w')), use

yourfile.write("The text that has to be in the file")
When you're done messing with the file, YOU HAVE TO USE

yourfile.close()
This should work, good luck with your program :P/>/>
Grim Reaper #3
Posted 17 May 2012 - 11:55 PM
From my understanding,

os.pullEventRaw()

Is essentially os.pullEvent() but does not accept the "Terminate" event.

os.pullEvent() and os.pullEventRaw() both operate based on a concept called "events". An event is just something that "happens", I suppose would be one way to explain it. When this event happens an event is queued and is waiting to be handled or disregarded.

Pulling the "event" allows us to handle it and its parameters.

For example if we wanted to check for a redstone signal to our computer, we would write some code that looks somewhat like this:

while true do -- Infinite loop :P/>/>
event, param1, param2 = os.pullEventRaw()-- This raw piece just means that the computer will not terminate the program
-- if the "Terminate" event is queued.

if event == "redstone" then break end -- Here we check to see if the variable event contains the case
-- "redstone", or a redstone signal. param1 and param2 are just extra variables in the case of other data or arguments
-- received with the event.

-- Pseudo:
--[[
  infinitely run this code
   set event, param1, and param2 to whatever event is next triggered by the computer or outside world, but don't
   allow terminate!
  
   if the event was a redstone signal then break our infinite loop
]]--
end

Hope I helped!