1

I workin' with Torch7 and Lua programming languages. I need a command that redirects the output of my console to a file, instead of printing it into my shell. For example, in Linux, when you type:

$ ls > dir.txt

The system will print the output of the command "ls" to the file dir.txt, instead of printing it to the default output console. I need a similar command for Lua. Does anyone know it?

[EDIT] An user suggests to me that this operation is called piping. So, the question should be: "How to make piping in Lua?"

[EDIT2] I would use this # command to do:

$ torch 'my_program' # printed_output.txt

DavideChicco.it
  • 3,318
  • 13
  • 56
  • 84
  • 2
    I believe this is usually called "piping" the output to file. Just mentioning this as it might help you find relevant results in web/SO searches. – jbaums Feb 06 '14 at 01:55
  • 1
    can you give an example code showing how you would use this in Lua? – Oliver Feb 06 '14 at 02:59
  • Duplicate of http://stackoverflow.com/questions/17636812/read-console-output-realtime-in-lua?rq=1 – AStopher Feb 06 '14 at 11:46

2 Answers2

2

Have a look here -> http://www.lua.org/pil/21.1.html

io.write seems to be what you are looking for.

jbaums
  • 27,115
  • 5
  • 79
  • 119
taka0
  • 27
  • 6
0

Lua has no default function to create a file from the console output. If your applications logs its output -which you're probably trying to do-, it will only be possible to do this by modifying the Lua C++ source code.

If your internal system has access to the output of the console, you could do something similar to this (and set it on a timer, so it runs every 25ms or so):

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

Note that the console output function has to store the output of the console in a table. To clear the console at the end, just do os.execute( "cls" ).

AStopher
  • 4,207
  • 11
  • 50
  • 75