lua-users home
lua-l archive

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



Niccolo Medici <niccolomedici@gmail.com>于2018年6月22日周五 上午12:23写道:
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).

You can write a small C library to call dup2() to redirect stdout to a file, if you are using Linux.