是否可以从FILE *抢救文件描述符? [英] Is it possible to rescue file descriptor from FILE*?

查看:99
本文介绍了是否可以从FILE *抢救文件描述符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用某个跨平台的库,该库可以传递FILE*个对象.

I have to use a certain cross-platform library which passes FILE* objects around.

我从另一个来源(继承)获得了文件描述符,我想在fork个进程中保持相同的fd .

I get a file descriptor from another source (inherited), I want to keep same fd across fork'd processes.

我目前使用fdopen将文件描述符转换为FILE*对象.

I currently use fdopen to convert a file descriptor to a FILE* object.

我的问题是,用于清理FILE*对象的fclose关闭了连接的文件描述符.

My problem is that fclose used to clean up FILE* objects closes connected file descriptor.

我非常想在使用该文件描述符后保留它.

I would very much like to keep this file descriptor after it has been used.

有没有办法从FILE*抢救文件描述符?

is there a way rescue file descriptor from FILE*?

有没有办法拆开它?

还是用假人替换FILE*中的文件描述符的方法?

Or a way to substitute a file descriptor in FILE* with a dummy?

P.S.无论如何,它都必须跨平台,并且跨POSIX.

P.S. this needs to be cross-platform, well across POSIX anyway.

推荐答案

我的问题是用于清理FILE*对象的fclose关闭 连接的文件描述符.

My problem is that fclose used to clean up FILE* objects closes connected file descriptor.

您可以使用 dup(2) 来获取描述符.然后fclose(3)不会执行的close(2)不会做任何事情.

You could use dup(2) to get a copy of the descriptor. Then the close(2) that fclose(3) does won't do anything.

我需要保持完全相同的fd编号

I need to maintain exact same fd number

然后在fclose之后再次调用dup2:dup2(savedfd, rescuedfd)

Then call dup2 again after fclose: dup2(savedfd, rescuedfd)

这篇关于是否可以从FILE *抢救文件描述符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆