如何在文件结束前阅读?

Fáb*_*rez 1 lua input eof

在C中,我可以读取输入并在程序到达文件末尾时停止程序(EOF).像这样.

#include <stdio.h>

int main(void) {
    int a;       
    while (scanf("%d", &a) != EOF)
        printf("%d\n", a);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我怎么能在Lua那样做?

Rio*_*ams 7

Lua的文档拥有一吨的对文件的阅读和其他IO细节.用于读取整个文件:

t = io.read("*all")
Run Code Online (Sandbox Code Playgroud)

显然是读取整个文件.文档中有逐行阅读的例子.希望这会有所帮助.

读取文件的所有行并对每个行进行编号的示例(逐行):

   local count = 1
    while true do
      local line = io.read()
      if line == nil then break end
      io.write(string.format("%6d  ", count), line, "\n")
      count = count + 1
    end
Run Code Online (Sandbox Code Playgroud)