简体   繁体   English

创建一个文件描述符

[英]Create a file descriptor

I want to create a file descriptor in C whose value i will specify in code. 我想在C中创建一个文件描述符,我将在代码中指定其值。 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" . 例如,我可能需要一个值为5的文件描述符,然后将其与名为“ sample.dat”的文件相关联。

fd = open ("sample.dat", O_RDONLY); open the file 打开文件

dup2 (fd, 5); and copy the file descriptor fd into the descriptor number 5 并将文件描述符fd复制到描述符编号5中

now you can do read (5, buffer, BUFF_MAX); 现在您可以read (5, buffer, BUFF_MAX); or also use fd to access the same file. 或者也可以使用fd访问相同的文件。 You need to close the fd explicitly if you do not need it. 如果不需要,则需要显式关闭fd

As @Arkadiy told see man dup2 for details. 正如@Arkadiy告诉的那样,请参阅man dup2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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