在 C 中,我可以读取输入并在到达文件末尾 (EOF
) 时停止程序。就像这样。
#include <stdio.h>
int main(void) {
int a;
while (scanf("%d", &a) != EOF)
printf("%d\n", a);
return 0;
}
我怎样才能在 Lua 中做到这一点?
最佳答案
Lua Documentation包含大量关于文件读取和其他 IO 的详细信息。读取整个文件:
t = io.read("*all")
显然读取了整个文件。该文档包含有关逐行阅读等的示例。希望对您有所帮助。
读取文件所有行并为每一行编号(逐行)的示例:
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
关于input - 我如何阅读直到文件结束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5094417/