Embedded Lua in C take input from STDIN
I'm writing a C app that has an embedded lua script in it. 开发者_如何学C The Lua script takes input from STDIN. So when I'm running the script from the shell it's like thus:
lua myscript.lua < datafile*
How do I accomplish this from inside the C code?
Thank you.
Use the dup2(2)
system call on descriptor 0
(stdin) and on the descriptor returned by open(2)
on datafile
:
int fd = open("datafile", O_RDONLY);
dup2(fd, 0);
/* reading from stdin now is in fact reading from datafile */
Of course, you need some error-checking in a real-world program.
To implement the behaviour of wildcarding, you may want to look at the readdir(3)
library function.
Have you tried to just run your script unmodified (i.e. use io.stdin
etc.)? Unless you're doing something fancy on C side, it should work out of the box.
精彩评论