当前位置:网站首页>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 STR function
- 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
- Solr series of full-text search engines - basic principles of full-text search
- Implement Gobang with C language
- 如何查询淘宝天猫的宝贝类目
- Zhonggan micro sprint technology innovation board: annual revenue of 240million, net loss of 17.82 million, proposed to raise 600million
- NOI OPENJUDGE 1.3(06)
- MySQL multi table query subquery
- 数学常数表 by q779
- 洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解
猜你喜欢

7-18 finding the single root of polynomial by dichotomy

C language to realize mine sweeping

Use of form text box (I) select text

On MEM series functions of C language

Sub-GHz无线解决方案Z-Wave 800 系列ZG23 soc和ZGM230S模块

Sub GHz wireless solution Z-Wave 800 Series zg23 SOC and zgm230s modules

Protobuf and grpc

Bibit pharmaceutical rushed to the scientific innovation board: annual revenue of 970000, loss of 137million, proposed to raise 2billion

基因家族特征分析 - 染色体定位分析

ShowMeBug入驻腾讯会议,开启专业级技术面试时代
随机推荐
Add ZABBIX calculation type itemcalculated items
Zzuli:1053 sine function
Code writing and playing method of tonybot humanoid robot at fixed distance
adc128s022 ADC verilog设计实现
Accelerating strategy learning using parallel differentiable simulation
NOI OPENJUDGE 1.4(15)
【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?
puzzle(016.3)千丝万缕
Talking about part of data storage in C language
556. The next larger element III
tonybot 人形機器人 紅外遙控玩法 0630
DDK for XP
动态获取权限
Luogu p5018 [noip2018 popularization group] symmetric binary tree problem solution
Zzuli:1059 highest score
Sub-GHz无线解决方案Z-Wave 800 系列ZG23 soc和ZGM230S模块
7-9 one way in, two ways out (25 points)
Find specified characters
Common shortcut keys in PCB
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