![](/img/trans.png)
[英]Reading standard(console) input from file and printing output to other file
[英]In Lua, how to print the console output into a file (piping) instead of using the standard output?
我使用Torch7和Lua编程语言。 我需要一个命令,它将控制台的输出重定向到一个文件 ,而不是将其打印到我的shell中。 例如,在Linux中,当您键入:
$ ls> dir.txt
系统会将命令“ ls”的输出打印到文件dir.txt,而不是将其打印到默认输出控制台。 我需要Lua的类似命令。 有人知道吗?
[编辑]用户向我建议此操作称为管道 。 因此,问题应该是:“如何在Lua中制作管道?”
[EDIT2]我将使用此#命令来执行以下操作:
$火炬'my_program'#printed_output.txt
在这里看看-> http://www.lua.org/pil/21.1.html
io.write
似乎是您想要的。
Lua没有默认功能,无法从控制台输出创建文件。 如果您的应用程序记录了它的输出(您可能正在尝试这样做),则只有通过修改Lua C ++源代码才能做到这一点。
如果您的内部系统可以访问控制台的输出,则可以执行类似的操作(并在计时器上进行设置,因此它每25ms左右运行一次):
dumpoutput = function()
local file = io.write([path to file dump here], "w+")
for i, line in ipairs ([console output function]) do
file:write("\n"..line);
end
end
请注意,控制台输出功能必须将控制台的输出存储在表中 。 要最后清除控制台,只需执行os.execute( "cls" )
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.