当前位置:网站首页>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
边栏推荐
- Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
- 关于敏捷的一些概念
- pyQt界面制作(登录+跳转页面)
- Zzuli:1046 product of odd numbers
- Bucket sorting in C language
- 添加Zabbix计算类型项目Calculated items
- Stop asking yourself if you are suitable for software testing
- Detailed explanation of four modes of distributed transaction (Seata)
- tonybot 人形机器人 查看端口并对应端口 0701
- Zzuli:1057 prime number determination
猜你喜欢
基因家族特征分析 - 染色体定位分析
使用并行可微模拟加速策略学习
Sub GHz wireless solution Z-Wave 800 Series zg23 SOC and zgm230s modules
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
Timecho of Tianmou technology completed an angel round financing of nearly 100 million yuan to create a native timing database of the industrial Internet of things
Dllexport et dllimport
adc128s022 ADC verilog设计实现
Doris学习笔记之数据表的创建
Amazon, express, lazada, shopee, eBay, wish, Wal Mart, Alibaba international, meikeduo and other cross-border e-commerce platforms evaluate how Ziyang account can seize traffic by using products in th
pyQt界面制作(登录+跳转页面)
随机推荐
Tonybot humanoid robot infrared remote control play 0630
Puzzle (016.4) domino effect
Zzuli:1041 sum of sequence 2
Luogu p4047 [jsoi2010] tribal division solution
NOI OPENJUDGE 1.4(15)
puzzle(016.3)千丝万缕
Tonybot Humanoïde Robot Infrared Remote play 0630
556. 下一个更大元素 III : 简单构造模拟题
Find books ()
DDK for XP
Protobuf and grpc
String reverse order
Find the sum of the elements of each row of the matrix
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
7-9 one way in, two ways out (25 points)
牛客 BM83 字符串变形(大小写转换,字符串反转,字符串替换)
Sword finger offer 28 Symmetric binary tree
7-20 print 99 formula table (format output)
洛谷P5194 [USACO05DEC]Scales S 题解
7-3 count the number of words in a line of text