How to redirect stdout to a file in Lua?

I am trying to redirect stdout in Lua (5.1) to a file instead of the console.

There is a third-party API (which I cannot change) that contains a function that outputs a serialized dataset (I don’t know which function does print, suppose some kind of print ())

This data is too detailed to fit on the screen that I have to work with (which cannot be scrolled), so I want the output of the function to be directed to a file and not to the console.

I have no way to fix or manipulate versions of Lua.

My thought was to change stdout to a file using the poorly documented io.output () file, but that doesn't work at all.

 io.output("foo")   -- creates file "foo", should set stdout to "foo"?
 print("testing. 1, 2, 3") -- should print into "foo", goes to console instead

Does anyone know of any way to force functions to be output to a file, or to force all stdout to a file instead of a console? TIA.

+4
source share
1 answer

You need to use io.write instead print. It works in a similar way, but does not share tabbed options. io.writeconsistent io.outputbut printnot working.

+2
source

All Articles