Create a file descriptor

坚强是说给别人听的谎言 提交于 2019-12-10 11:44:48

问题


I want to create a file descriptor in C whose value i will specify in code. I have a integer variable which specifies the value of file descriptor to be created. For example i may need a file descriptor whose value is 5 and later associate that with the file named "sample.dat" .


回答1:


You need dup2()

http://linux.die.net/man/2/dup




回答2:


fd = open ("sample.dat", O_RDONLY); open the file

dup2 (fd, 5); and copy the file descriptor fd into the descriptor number 5

now you can do read (5, buffer, BUFF_MAX); or also use fd to access the same file. You need to close the fd explicitly if you do not need it.

As @Arkadiy told see man dup2 for details.



来源:https://stackoverflow.com/questions/5935872/create-a-file-descriptor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!