当前位置:网站首页>C language DUP function
C language DUP function
2022-07-03 14:40:00 【ma_ de_ hao_ mei_ le】
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, char *argv[])
{
// Copy file descriptor
// The descriptor encoding is up to 1023
// 0 1 2 Occupied by the system , These are standard inputs 、 standard output 、 The standard error
// Other file descriptors must be from 3 Start
// A file can be pointed to by multiple file descriptors
// dup The function returns a file descriptor , It will select the smallest of the idle file descriptors middle note as the new file descriptor returned
int fd = open("a.txt", O_RDWR | O_CREAT, 0664);
int fd1 = dup(fd);
if (fd1 == -1) {
perror("open");
return -1;
}
printf("fd: %d, fd1: %d\n", fd, fd1);
close(fd);
char* str = "hello world";
int ret = write(fd1, str, strlen(str));
if (ret == -1) {
perror("write");
return -1;
}
return 0;
}
The above code will use dup Function will a.txt File descriptor for fd Copied to the fd1, After copying , Both file descriptors point to a.txt, operation fd and fd1 Any file descriptor is equivalent to an operation a.txt
a.txt Is written in hello world
边栏推荐
- Sword finger offer 28 Symmetric binary tree
- The mail function of LNMP environment cannot send mail
- 如何查询淘宝天猫的宝贝类目
- Zzuli:1046 product of odd numbers
- Zzuli:1049 square sum and cubic sum
- C language to implement a password manager (under update)
- ConstraintLayout 的使用
- Accelerating strategy learning using parallel differentiable simulation
- Luogu p5018 [noip2018 popularization group] symmetric binary tree problem solution
- Find books ()
猜你喜欢

tonybot 人形机器人 定距移动 代码编写玩法

tonybot 人形机器人 首次开机 0630

分布式事务(Seata) 四大模式详解
![洛谷P4047 [JSOI2010]部落划分 题解](/img/7f/3fab3e94abef3da1f5652db35361df.png)
洛谷P4047 [JSOI2010]部落划分 题解

Happy capital new dual currency fund nearly 4billion yuan completed its first account closing

NPM install is stuck with various strange errors of node NPY

Paper sharing: generating playful palettes from images

Accelerating strategy learning using parallel differentiable simulation

亚马逊、速卖通、Lazada、Shopee、eBay、wish、沃尔玛、阿里国际、美客多等跨境电商平台,测评自养号该如何利用产品上新期抓住流量?

Frequently asked questions: PHP LDAP_ add(): Add: Undefined attribute type in
随机推荐
Time conversion ()
tonybot 人形机器人 红外遥控玩法 0630
Sub-GHz无线解决方案Z-Wave 800 系列ZG23 soc和ZGM230S模块
Find specified characters
Dllexport et dllimport
Zzuli:1059 highest score
How to query the baby category of tmall on Taobao
Implement Gobang with C language
Tonybot humanoid robot infrared remote control play 0630
SSH access control, blocking the IP when logging in repeatedly to prevent brute force cracking
tonybot 人形机器人 定距移动 代码编写玩法
Tonybot humanoid robot starts for the first time 0630
Zzuli:1044 failure rate
7-24 reduction of the simplest fraction (rolling Division)
7-10 stack of hats (25 points) (C language solution)
Thread.sleep和TimeUnit.SECONDS.sleep的区别
Zzuli:1056 lucky numbers
How Facebook moves instagram from AWS to its own server
7-3 rental (20 points)
Detailed explanation of four modes of distributed transaction (Seata)