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" .
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
You need dup2()
http://linux.die.net/man/2/dup
fd = open ("sample.dat", O_RDONLY);
open the filedup2 (fd, 5);
and copy the file descriptorfd
into the descriptor number 5now you can do
read (5, buffer, BUFF_MAX);
or also usefd
to access the same file. You need to close thefd
explicitly if you do not need it.As @Arkadiy told see
man dup2
for details.