FS API ã使ãããªãã(4): fs.open()ã§ãã¡ã¤ã«ã®èªã¿æ¸ã
ããã¾ã§ã®ã話
ååã¾ã§ã«ComputerCraftã§ãã¡ã¤ã«ãæ±ãããã®åºç¤ç¥èãç´¹ä»ãã¾ããã
- FS API を使いこなそう(1): CCファイルシステムの基礎
- FS API を使いこなそう(2): CCのファイルを扱うシステムプログラムの紹介
- FS API を使いこなそう(3): FS APIの解説
ä»åã¯ãFS APIã®fs.open()é¢æ°ã®èª¬æãéãã¦å®éã«ãã¡ã¤ã«ãèªã¿æ¸ããã¦ã¿ã¾ãããã
ã¾ãã¯ãfs.open()é¢æ°ã®ä»æ§ã«ã¤ãã¦ã
fs.open()é¢æ°
ä»æ§
- fs.open(file_path, mode)
- æå®ãããã¢ã¼ã(mode)ã§ãã¡ã¤ã«(file_path)ãéãããã¡ã¤ã«ãã³ãã«ãè¿ããä»å¾ã¯ãã®ãã¡ã¤ã«ãã³ãã«ãéãã¦ãã¡ã¤ã«ãèªã¿æ¸ãããã
- å¼æ°ï¼ï¼æååï¼ï¼ããã¡ã¤ã«ãã¹
- å¼æ°ï¼ï¼æååï¼ï¼ãã¢ã¼ãã®ç¨®é¡ã«ã¤ãã¦ã¯å¾è¿°ãåºæ¬çã«ã¯"r"ã"w"ã"a"ã®3ã¤ãç解ãã¦ä½¿ãããªãã°ãããã¨ï¼æ¥µè«ï¼
- è¿å¤ï¼ãã¼ãã«ï¼ï¼ããã¡ã¤ã«ãã³ãã«ãLuaã§ã¯ãã®ãã¡ã¤ã«ãåãæ±ãããã®é¢æ°ãå ¥ã£ããã¼ãã«ã
ããã¹ããã¡ã¤ã«ãèªã¿æ¸ãããããã®ã¢ã¼ã
ã¢ã¼ã | æ¦è¦ |
---|---|
"r" | ãã¡ã¤ã«ãèªã¿å°ç¨ï¼readï¼ã¢ã¼ãã§éãã |
"w" | ãã¡ã¤ã«ãæ°è¦æ¸ãè¾¼ã¿(write)ã¢ã¼ãã§éãã æ¢åãã¡ã¤ã«ã®ä¸èº«ãæ¶ãã¦æ°è¦ã«æ¸ãè¾¼ããã¨ã«æ³¨æã |
"a" | ãã¡ã¤ã«ã追å æ¸ãè¾¼ã¿(add)ã¢ã¼ãã§éãã æ¢åãã¡ã¤ã«ã®æå¾ã«è¨è¿°ã追å ãã¦ããã |
- ä¸è¨3ã¤ã®ã¢ã¼ãã¯ãããã¹ããã¡ã¤ã«ï¼äººéãèªãããã¡ã¤ã«ï¼ãæ±ãããã®ã¢ã¼ãã§ããããããã¹ãã¢ã¼ããã¨å¼ã°ãã¾ãã
- ããã«å¯¾ãã¦ç»åãã¡ã¤ã«ãé³å£°ãã¡ã¤ã«ãªã©ãããã¹ããã¡ã¤ã«ä»¥å¤ã®ãã¡ã¤ã«ããã¤ããªãã¡ã¤ã«ã¨å¼ã³ããã¤ããªãã¡ã¤ã«ãåãæ±ãã¢ã¼ããããã¤ããªã¢ã¼ããã¨å¼ã³ã¾ãã
- ãã¤ããªãã¡ã¤ã«ãéãã¦ä¸ãè¦ãã¦ããä¸è¬äººã«ã¨ã£ã¦ã¯æå³ä¸æãªæå(æ°å¤)ãã表示ããã¾ããã
- ä¸è¨3ã¤ã®ã¢ã¼ãã«"b"ãã¤ããã"rb"ã"wb"ã"ab"ããã¤ããªã¢ã¼ãã§ãã¡ã¤ã«ãéãããã®è¨å·ã§ãã
ã©ã®ã¢ã¼ããæå®ãããã«ãã£ã¦ããã¡ã¤ã«ãã³ãã«ã§ç¨æãããé¢æ°ãå¤ãã£ã¦ãã¾ãã
ãã¨ãã°"r"ã¢ã¼ãã§ãã¡ã¤ã«ãéãã¨ãè¿ã£ã¦ãããã¡ã¤ã«ãã³ãã«ï¼ãã¼ãã«ï¼ã«ã¯ãã¡ã¤ã«ãèªãããã®é¢æ°ãå ¥ã£ã¦ãããæ¸ãããããã®é¢æ°ãå«ã¾ãã¦ãã¾ããã
ãã¡ã¤ã«ãã³ãã«ã£ã¦ï¼
ä¸è¨ã§ãããªãã°ããã®ãã¡ã¤ã«ãæ軽ã«åãæ±ãããã«ã·ã¹ãã ãç¨æããã¤ã³ã¿ãã§ã¼ã¹ã§ãã
ãã¨ãã°è»ã®æä½ã§è¨ãã¨ãéè·¯ãå³æããããã«ã¯ã¿ã¤ã¤ãé©åãªã¿ã¤ãã³ã°ã§é©åãªè§åº¦ã«åãæ¿ããªããã°ãªãã¾ãããããã³ãã«ã¨ããã·ã³ãã«ãªã¤ã³ã¿ãã§ã¼ã¹ã使ããã¨ã«ãã£ã¦ãé転ã®ç´ 人ã§ãæ¯è¼çç°¡åã«æ²ãããã¨ãã§ãã¾ãã
ï¼ESTIMAã®ãã³ãã« / æç¿° / ãã¼ãã³ / POHANï¼
ãã¡ã¤ã«ã®æä½ãåæ§ã§ããå®éã«ãã¡ã¤ã«ãèªãã ãæ¸ãããããå ·ä½çãªåä½ãããã°ã©ãã³ã°ããã¨ãªãã¨ããªã大å¤ãªãã¨ãªã®ã§ããããã¡ã¤ã«ãã³ãã«ã¨ããã¤ã³ã¿ãã§ã¼ã¹ã使ããã¨ã«ãã£ã¦ç°¡åã«æä½ã§ããã®ã§ãã
ComputerCraftã®fs.open()é¢æ°ã«ãã£ã¦æã«å ¥ããã¡ã¤ã«ãã³ãã«ããã®å®ä½ã¯ãã ã®ãã¼ãã«ã§ããä¸ã«ã¯éãããã¡ã¤ã«ãæä½ããããã®é¢æ°ãå ¥ã£ã¦ããã®ã§ããã®é¢æ°ã使ãã ãã§ç´°ãããã¨ã«åããããã¨ãªããããããã¨ã«éä¸ã§ãã¾ãã
åã¢ã¼ãã§ã©ã®ãããªãã¡ã¤ã«ãã³ãã«ãã§ããã®ã
"r"ãèªã¿å°ç¨ã¢ã¼ãã§éããã¨ã
fs.open()ã§æå®ãããã¡ã¤ã«ãåå¨ããªãã¨ãã«ã¯èªã¿å°ç¨ã¢ã¼ãã§ãã¡ã¤ã«ãéããã¨ã¯ã§ãã¾ããã
ããã§ã¯ãmyfileã¨ããããã¹ããã¡ã¤ã«ããã§ã«åå¨ãã¦ããã¨ãã¾ãã
使ç¨ä¾
# lua interactive mode (> lua) lua> fh = fs.open("myfile", "r") lua> fh table: 73e2e8da lua> for k,v in pairs(fh) do print(k) end readLine readAll close
ã©ãããèªã¿å°ç¨ï¼"r"ï¼ã¢ã¼ãã§ã¯ã3ã¤ã®é¢æ°ãå ¥ã£ã¦ããããã§ããããããç°¡åã«ä½¿ãæ¹ã説æãã¾ãããã
readLine()ï¼ããã¡ã¤ã«ãã1è¡èªã¿åºã
- readLine()
- éãããã¡ã¤ã«ãã1è¡åã®æååãåãåºãã¾ããå®è¡ãããã³ã«ã2è¡ç®ã»3è¡ç®ã¨æ¬¡ã®è¡ã®æååãè¿ã£ã¦ãã¾ãã
- ãªããæçµè¡ã¾ã§è¡ã£ããnilãè¿ã£ã¦ãã¾ãã
- è¿å¤ï¼æååï¼ï¼ã1è¡åã®æåå
使ãæ¹
# "myfile"ã®ä¸èº«ãå ¨ã¦ç»é¢ã«è¡¨ç¤ºããããã°ã©ã local fh = fs.open("myfile", "r") repeat local str = fh.readLine() print(str) until not str
readAll()ï¼ããã¡ã¤ã«ããå ¨ã¦ã®æåãèªã¿åºã
- readAll()
- éãããã¡ã¤ã«ããæ¹è¡ãå«ãã¦ä¸æ°ã«å ¨ã¦ã®æååãåãåºãã¾ãã
- ãªããä¸åº¦å®è¡ãã¦èªã¿è¾¼ãã å¾ã¯ãå度å®è¡ãã¦ãé·ã0ã®æååããè¿ã£ã¦æ¥ã¾ããã
- è¿å¤ï¼æååï¼ï¼ãæ¹è¡ãå«ãã¦å ¨æåå
使ãæ¹
# "myfile"ã®ä¸èº«ãå ¨ã¦ç»é¢ã«è¡¨ç¤ºããããã°ã©ã local fh = fs.open("myfile", "r") print(fh.readAll())
close()ï¼ãéãããã¡ã¤ã«ãéãã
- close()
- éãããã¡ã¤ã«ãéããããä½æ¥ãçµãã£ããéãããã¨ããã®ã¯ãã¡ã¤ã«åãæ±ãã®ããã¼ã§ãã
- ãã¡ã¤ã«ãèªã"r"ã¢ã¼ãã§ã¯ãã»ã©åé¡ã«ãªãã¾ããããæ¸ãè¾¼ã"w","a"ã¢ã¼ãã§éãå¿ããã¨é¢åãªãã¨ã«ï¼ï¼ãå¿ããªãããã«ã
ãã¢ã¯éãããéãã¾ãããï¼ãããã¨åãã§ãã
ï¼Door Handle / Sean MacEnteeï¼
local fh = fs.open("myfile", "r") # ããããã¨ãã¡ã¤ã«ãåãæ±ã£ãã㨠ã»ã»ã» # çµäºï¼ãããã¼ã§ããï¼ï¼ fh.close()
"w" æ°è¦æ¸ãè¾¼ã¿ã¢ã¼ãã§éããã¨ã
fs.open()ã§æå®ãããã¡ã¤ã«ããã§ã«åå¨ãã¦ããã¨ãããã®ãã¡ã¤ã«ã®ä¸èº«ãæ¶ãã¦æ°ããä¸æ¸ããã¦ãã¾ããã¨ã«æ³¨æãã¦ãã ããã
ï¼ããã®ãã¹ãããã¨çããçãããã®ã§ããã»ã»ã»ã
# lua interaction mode (> lua) lua> fh = fs.open("myfile", "w") lua> fh table: 2d4e490d lua> for k,v in pairs(fh) do print(k) end write writeLine flush close
ã©ãããæ°è¦æ¸ãè¾¼ã¿ï¼"w"ï¼ã¢ã¼ãã§ã¯ã4ã¤ã®é¢æ°ãå ¥ã£ã¦ããããã§ããããããç°¡åã«ä½¿ãæ¹ã説æãã¾ãããã
write()ï¼ããã¡ã¤ã«ã«æååãæ¸ãè¾¼ã
- write(str)
- ãã¡ã¤ã«ã«æååãæ¸ãè¾¼ã
- å¼æ°ï¼æååï¼ï¼ãæ¸ãè¾¼ãæåå
- è¿å¤ãªã
writeLine()ï¼ããã¡ã¤ã«ã«æååãæ¸ãè¾¼ã¿ãããã«æ¹è¡ãã
- writeLine(str)
- ãã¡ã¤ã«ã«æååãæ¸ãè¾¼ã¿ããããã®å¾ã«æ¹è¡ããï¼æ¹è¡ã³ã¼ããåãè¾¼ãï¼
- å¼æ°ï¼æååï¼ï¼ãæ¸ãè¾¼ãæåå
- è¿å¤ãªã
ããã§æ³¨æããªãã¦ã¯ãªããªãã®ã¯ãä¸è¨ã®é¢æ°ã§æååãæ¸ãè¾¼ãã§ããå®éã«ã¯ä»¥ä¸ã®é¢æ°ã使ããªãã¨ãã¡ã¤ã«ã«åæ ãããªãã¨ãããã¨ã§ã*1
flush()ï¼ãæ¸ãè¾¼ãã å 容ãå®éã®ãã¡ã¤ã«ã«åæ ãããï¼å®éã«æ¸ãè¾¼ãï¼
- flush()
- write()ãwriteLine()ã§æ¸ãè¾¼ãã å 容ãå®éã®ãã¡ã¤ã«ã«æ¸ãè¾¼ããéã«è¨ãã¨ããã®é¢æ°ãå®è¡ããã¾ã§å®éã«ã¯æ¸ãè¾¼ã¾ããªãã
close()ï¼ãéãããã¡ã¤ã«ãéãã
- close()
- éãããéãããããã¼ã§ããï¼ããªããã¾ã ãã¡ã¤ã«ã«åæ ããã¦ããªãæ¸ãè¾¼ã¿ãæ®ã£ã¦ãããæ¸ãè¾¼ãã§ããã¾ããã¤ã¾ãflush()å¿ãã¦ãã¦ãclose()ãããã¦ããã°ï¼ããç¨åº¦ã¯ï¼å¤§ä¸å¤«ï¼
使ç¨ä¾
local fh = fs.open("newfile", "w") fh.writeLine("Hello World!") fh.flush() -- ããã¾ã§ã®å 容ãä¸æ¦æ¸ã込㿠fh.write("H") fh.write("e") fh.write("l") fh.write("l") fh.write("o") fh.close() -- æ¸ãè¾¼ã¿çµäºï¼
æ°ããæ¸ãè¾¼ã¾ãããã¡ã¤ã«ãnewfileãã®å 容ã¯æ¬¡ã®éãã§ãã
Hello World! Hello
"a" 追å æ¸ãè¾¼ã¿ã¢ã¼ãã§éããã¨ã
fs.open()ã§æå®ãããã¡ã¤ã«ããã§ã«åå¨ãã¦ããã¨ãããã®ãã¡ã¤ã«ã®æ«å°¾ã«ç¶ãã¦æ¸ãè¾¼ã¾ãã¾ãã
æå®ãããã¡ã¤ã«ãåå¨ããªããã°æ°ãããã¡ã¤ã«ãä½ã£ã¦ããã«æ¸ãè¾¼ã¿ã¾ãã
# lua interaction mode (> lua) lua> fh = fs.open("newfile", "a") lua> fh table: 6a2cbdcd lua> for k,v in pairs(fh) do print(k) end write writeLine flush close
ç¨æãããé¢æ°ã¯ãæ°è¦æ¸ãè¾¼ã¿ã¢ã¼ã"w"ã¨åãã§ãããã®ãã説æãçãã¦ä½¿ç¨ä¾ã ãã
-- ãã¡ã¤ã«ãnewfileãã¯ããã»ã©ã®"w"ã¢ã¼ãã§ä½ã£ããã® local fh = fs.open("newfile", "a") fh.writeLine(" Wrold 2") fh.write("Hello World 3") fh.close()
追å æ¸ãè¾¼ã¿ããããã¡ã¤ã«ãnewfileãã®å 容ã¯æ¬¡ã®ããã«ãªãã¾ãã
Hello World! Hello World2 Hello World3
ï¼è£è¶³ï¼ãã¤ããªã¢ã¼ãã®å ´åã"rb"ã"wb"ã"ab"
ãã¤ããªãã¡ã¤ã«ãç´æ¥åãæ±ããã¨ã¯ã»ã¨ãã©ãªãã¨æãã¾ããã念ã®ããè£è¶³ã¨ãã¦ã
ãã¤ããªãã¡ã¤ã«ãªã®ã§ãæ¹è¡ã³ã¼ããªãã¦ç¹å¥æ±ãã¯ãã¾ããããã¹ã¦ã¯1ãã¤ããã¤ã®ãã¼ã¿ï¼
"rb" ãã¤ããªãã¡ã¤ã«èªã¿å°ç¨ã¢ã¼ã
- read()
- ãã¤ããªãã¡ã¤ã«ãã1ãã¤ãåã®ãã¼ã¿ãèªã¿è¾¼ã¿ã¾ããå®è¡ãããã³ã«æ¬¡ã ã¨1ãã¤ããã¤ãã¼ã¿ãèªã¿è¾¼ã¿ã¾ãã
- è¿å¤ï¼æ°å¤ï¼ï¼
"wb", "ab"ããã¤ããªãã¡ã¤ã«æ°è¦æ¸ãè¾¼ã¿ï¼è¿½å æ¸ãè¾¼ã¿ï¼ã¢ã¼ã
- write(num)
- å¼æ°ï¼æ°å¤ï¼ï¼ããã¡ã¤ã«ã«æ¸ãè¾¼ã¿ãã1ãã¤ã(byte)ã®æ°å¤
- è¿å¤ãªã
ãããã«
次åã¯ãããã¾ã§ç´¹ä»ããFS APIã®é¢æ°ã使ã£ã¦ä¾¿å©ãªããã°ã©ã ãç´¹ä»ãã¾ãããã
ã¨ããããä»ã®ã¨ããæ¡ã¨ãã¦æã£ã¦ããã®ã¯æ¬¡ããããä»ã«ããªã¯ã¨ã¹ããããã°ãç¥ãããã ããã
- ä¾åããã©ã¤ãã©ãªï¼APIãã¡ã¤ã«ï¼ãpastebinããèªåçã«ãã¦ã³ãã¼ãï¼ã¤ã³ã¹ãã¼ã«ãã¦ãããããã°ã©ã
- ã¿ã¼ãã«ãä½ãåä½ãè¡ããã³ã«ãåä½ã®ãã°ãã¨ã£ã¦ããããã°ã©ã
*1:ããã¯ããã¡ã¤ã«ãèªããã¨ããæ¸ãè¾¼ããã¨ã®ã»ããä½æ¥çãªè² è·ãé«ãããã§ããã¡ã¾ã¡ã¾ãã¡ã¤ã«ã«æ¸ãè¾¼ããªãã¦ã¾ã©ãã£ããããã¨ããªãã§ãããç¨åº¦ï¼ãããã¡ã«ï¼è²¯ãã¦ããã¦ããä¸æ°ã«æ¸ãè¾¼ãã¨ããå¹çãéè¦ããä»æ§ã§ãã