Create a file descriptor

2019-06-10 13:37发布

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" .

2条回答
做自己的国王
2楼-- · 2019-06-10 14:16
霸刀☆藐视天下
3楼-- · 2019-06-10 14:33

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.

查看更多
登录 后发表回答