我如何阅读直到文件末尾? [英] How do I read until the end of file?
本文介绍了我如何阅读直到文件末尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C语言中,我可以读取输入并在程序到达文件末尾(EOF
)时停止程序.像这样.
In C, I can read an input and stop the program when it reaches the end of file (EOF
). Like so.
#include <stdio.h>
int main(void) {
int a;
while (scanf("%d", &a) != EOF)
printf("%d\n", a);
return 0;
}
如何在Lua中做到这一点?
How can I do that in Lua?
推荐答案
Lua文档提供了大量有关文件读取和其他IO的详细信息.要读取整个文件:
The Lua Documentation features a ton of details on file-reading and other IO. For reading an entire file:
t = io.read("*all")
显然会读取整个文件.该文档提供了逐行阅读示例,希望对您有所帮助.
apparently reads an entire file. The documentation has examples on reading line-by-line etc. Hope this helps.
读取文件的所有行并为每个行编号(逐行)的示例:
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
这篇关于我如何阅读直到文件末尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文