<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                I/O庫為文件操作提供兩種模式。簡單模式(simple model)擁有一個當前輸入文件和一個當前輸出文件,并且提供針對這些文件相關的操作。完全模式(complete model)使用外部的文件句柄來實現。 ## 簡單模式 I/O庫將當前輸入文件作為標準輸入(stdin),將當前輸出文件作為標準輸出(stdout)。這樣當我們執行io.read,就是在標準輸入中讀取一行。 寫操作較讀操作簡單,我們先從寫操作入手。 下面這個例子里函數io.write獲取任意數目的字符串參數,接著將它們寫到當前的輸出文件。 ~~~ local t = io.write("sin (3) = ", math.sin(3), "\n") --> sin (3) = 0.1411200080598672 print("hello", "Lua"); print("Hi") -->hello Lua -->Hi ~~~ 注:Write函數與print函數不同在于,write不附加任何額外的字符到輸出中去,例如制表符,換行符等等。還有write函數是使用當前輸出文件,而print始終使用標準輸出。另外print函數會自動調用參數的tostring方法,所以可以顯示出表(tables)函數(functions)和nil。 read函數:從當前輸入文件讀取串,由它的參數控制讀取的內容: ![](https://box.kancloud.cn/2016-02-22_56cb2ca2ad621.jpg) 例子: ~~~ --io.read 從標準輸入流中獲得,默認設置下,就是你的屏幕輸入 t = io.read("*all") t = string.gsub(t, ...) -- do the job io.write(t) -- write the ~~~ 提示:若使用luaEditor編輯器,估計無法在屏幕輸入。 ## 完全模式 完全模式的核心在于文件句柄(file handle)。該結構類似于C語言中的文件流(FILE*),其呈現了一個打開的文件以及當前存取位置。打開一個文件的函數是io.open。它模仿C語言中的fopen函數,同樣需要打開文件的文件名參數,打開模式的字符串參數: ![](https://box.kancloud.cn/2016-02-22_56cb2ca2bc20e.jpg) 例子: ~~~ --讀操作 file = io.open("testRead.txt", "r") for line in file:lines() do print(line) end file:close() --寫操作 file = io.open("testRead.txt","a+") file:write("\nhello") file:close() ~~~ 素材: ![](https://box.kancloud.cn/2016-02-22_56cb2ca2cc9ea.jpg) 內容: ![](https://box.kancloud.cn/2016-02-22_56cb2ca2dec7b.jpg) 運行結果: ![](https://box.kancloud.cn/2016-02-22_56cb2ca2ed7b8.jpg) 文件內容: ![](https://box.kancloud.cn/2016-02-22_56cb2ca30537e.jpg)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看