lua 学习笔记11

lua 学习笔记11

/*********************/
       I/O 系统
/*********************/
lua中,I/O分为简单i/o模型和完整i/o模型。
简单i/o模型实际上是完整i/o模型的免文件参数版,它默认了操作的对象是stdin和stdout。
i/o模型可以操作的函数有:
read
write
open
另外,还有一个我们常用于调试的print,它实际上是简单i/o模型的一种运用。
print和read的区别:
write adds no extra characters to the output, such as tabs or newlines. Moreover, write uses the current output file, whereas print always uses the standard output. Finally, print automatically applies tostring to its arguments, so it can also show tables, functions, and nil.
/******************/
   I/O 中的open
/******************/
简单I/O就是open的不是普通的文件,所以它可以直接使用io.read(),io.write()来操作.如果要使用完整的I/O操作,就必须要先open一个文件.
它的用法有:
io.open("filename","parameter")
filename: 文件名
parameter: 参数
That mode string may contain an `r′ for reading, a `w′ for writing (which also erases any previous content of the file), or an `a′ for appending, plus an optional `b′ to open binary files.( 和c一样参数可以写在一起)
例如:
    print(io.open("non-existent file", "r"))
      –> nil     No such file or directory       2
   
    print(io.open("/etc/passwd", "w"))
      –> nil   Permission denied       13
当文件open以后,就可以用"文件名:操作"了
例如:
    f = io.open("a.txt","r")
    f:read()
文件open以后,操作完毕时,要使用f:close()来关闭文件.也可以使用io.open():close()来关闭当前打开的一个open.
/********************/
    I/O 中的read
/********************/
read的参数列表
"*all" reads the whole file
"*line" reads the next line
"*number" reads a number
num reads a string with up to num characters
例如:
t = io.read("*all")
num参数的用法:
Besides the basic read patterns, you can call read with a number n as argument: In this case, read tries to read n characters from the input file. If it cannot read any character (end of file), read returns nil; otherwise, it returns a string with at most n characters. As an example of this read pattern, the following program is an efficient way (in Lua, of course) to copy a file from stdin to stdout:
    local size = 2^13      — good buffer size (8K)
    while true do
      local block = io.read(size)
      if not block then break end
      io.write(block)
    end
一个按照读取的例子:
Usually, in Lua, it is much faster to read a file as a whole than to read it line by line. However, sometimes we must face some big files (say, tens or hundreds megabytes) for which it is not reasonable to read them all at once. If you want to handle such big files with maximum performance, the fastest way is to read them in reasonably large chunks (e.g., 8 KB each). To avoid the problem of breaking lines in the middle, you simply ask to read a chunk plus a line:
    local lines, rest = f:read(BUFSIZE, "*line")
The variable rest will get the rest of any line broken by the chunk. We then concatenate the chunk and this rest of line. That way, the resulting chunk will always break at line boundaries.
A typical example of that technique is this implementation of wc, a program to count the number of characters, words, and lines in a file:
    local BUFSIZE = 2^13     — 8K
    local f = io.input(arg[1])   — open input file
    local cc, lc, wc = 0, 0, 0   — char, line, and word counts
    while true do
      local lines, rest = f:read(BUFSIZE, "*line")
      if not lines then break end
      if rest then lines = lines .. rest .. ‘n’ end
      cc = cc + string.len(lines)
      — count words in the chunk
      local _,t = string.gsub(lines, "%S+", "")
      wc = wc + t
      — count newlines in the chunk
      _,t = string.gsub(lines, "n", "n")
      lc = lc + t
    end
    print(lc, wc, cc)
 
/********************/
    I/O 中的write
/********************/
把要写的字符串放在write()内就好了

3 thoughts on “lua 学习笔记11

  1. wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold -229151942401788

  2. Hi,Do you have used LCDs, second hand LCDs, used flat screens and used LCD monitors? Please go here:www.sstar-hk.com(Southern Stars).We are constantly buying re-usable LCD panels and working for LCD recycling.The re-usable panels go through strictly designed process of categorizing, checking, testing, repairing and refurbishing before they are re-used to make remanufactured LCD displays and TV sets.Due to our recent breakthrough in testing and repairing technology of LCD, we can improve the value for your LCD panels.
    website:www.sstar-hk.com[igaabdidddihhg]

Leave a Reply

Your email address will not be published. Required fields are marked *