简体   繁体   中英

Is it possible to write to file descriptor opened in C from a Lua function?

我有一个小的C程序调用Lua函数并向它发送一个文件描述符,是否可以从Lua写入这个文件描述符?

Have a look at the C function fdopen(). It takes a file descriptor and gives you back a stream/FILE*.

If you implement a Lua C extension for fdopen() to give you back a LuaStream then you can use the normal Lua io calls on the resulting Lua Stream.

Better than just implementing a one-off function for what you want to do.

see io_open in liolib.c of the Lua 5.2.1 sources for an example of creating a Lua Stream.

我认为在调用lua函数之前将标准输出重定向到套接字fd可能会起作用,但是返回字符串并将其写入C更容易。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM