当前位置:网站首页>C language dup2 function
C language dup2 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[])
{
// dup2 function
// dup Function will directly return an unoccupied file descriptor ( The smallest file descriptor from idle )
// however dup2 The function will accept a fd As the specified file descriptor
// If the specified file descriptor is occupied , Then the file corresponding to the file descriptor will be closed
int fd = open("a.txt", O_RDWR | O_CREAT, 0664);
if (fd == -1) {
perror("open");
return -1;
}
int fd1 = open("b.txt", O_RDWR | O_CREAT, 0664);
if (fd1 == -1) {
perror("open");
return -1;
}
printf("fd: %d, fd1: %d\n", fd, fd1);
// here fd1 Will point to fd Files pointed to
// Then operate fd1 It's equivalent to operating a.txt
int fd2 = dup2(fd, fd1);
if( fd2 == -1) {
perror("dup2");
return -1;
}
char* str = "Hello, dup2";
int ret = write(fd1, str, strlen(str));
if (ret == -1) {
perror("write");
return -1;
}
printf("fd: %d, fd1: %d, fd2: %d\n", fd, fd1, fd2);
close(fd);
close(fd1);
return 0;
}
Compared with dup function ,dup2 Function can redirect a file descriptor to the file pointed to by another file descriptor
In the code above ,fd1 Redirected to fd The point is a.txt file , So right. fd1 The operation of the file descriptor is equivalent to the operation a.txt
in addition dup2 The return value of a function has no real effect , The return value can be regarded as a judgment dup2 Whether the function call is successful depends on
In this code ,fd2 The value of and fd1 It's consistent
边栏推荐
- C language to implement a password manager (under update)
- 一文了解微分段应用场景与实现机制
- Zzuli:1049 square sum and cubic sum
- 2021-10-16 initial programming
- Talking about part of data storage in C language
- Zzuli:1055 rabbit reproduction
- Zzuli:1041 sum of sequence 2
- 表单文本框的使用(一) 选择文本
- 1017 a divided by B (20 points)
- adc128s022 ADC verilog设计实现
猜你喜欢
![[qingniaochangping campus of Peking University] in the Internet industry, which positions are more popular as they get older?](/img/f6/fe61c84f289f0e74a45946dac687a6.jpg)
[qingniaochangping campus of Peking University] in the Internet industry, which positions are more popular as they get older?

Four data flows and cases of grpc

Creation of data table of Doris' learning notes

提高效率 Or 增加成本,开发人员应如何理解结对编程?

分布式事务(Seata) 四大模式详解

Dllexport et dllimport

Rasterization: a practical implementation (2)

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

Tonybot humanoid robot checks the port and corresponds to port 0701

MySQL multi table query subquery
随机推荐
[combinatorics] permutation and combination (set combination, one-to-one correspondence model analysis example)
FPGA blocking assignment and non blocking assignment
洛谷P5536 【XR-3】核心城市 题解
一文了解微分段应用场景与实现机制
Find the sum of the elements of each row of the matrix
洛谷P4047 [JSOI2010]部落划分 题解
Recent learning summary
Use of constraintlayout
SSH access control, blocking the IP when logging in repeatedly to prevent brute force cracking
Sub-GHz无线解决方案Z-Wave 800 系列ZG23 soc和ZGM230S模块
tonybot 人形機器人 紅外遙控玩法 0630
Four data flows and cases of grpc
NOI OPENJUDGE 1.6(09)
To improve efficiency or increase costs, how should developers understand pair programming?
Tonybot humanoid robot infrared remote control play 0630
洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解
7-28 monkeys choose King (Joseph problem)
Luogu p5194 [usaco05dec]scales s solution
556. The next larger element III
Some concepts about agile