lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I want to redirect the output of print() to a file of my choosing.

(I know I can do "$ lua program.lua > output.txt", but I need to
create several output files. My program is already written and I don't
want to modify it too much.)

I tried this:

    io.output('out.txt')
    print('hello world')

But this doesn't work (on both Lua 5.2 and 5.3): it creates an empty
file named "out.txt" but keeps sending the output to the terminal (I'm
using Linux, BTW).

The documentation for print() says it prints "to stdout", and the
documentation for io.output() says that it "opens the named file and
sets its handle as the default output file". So I don't undertand why
my code doesn't work.