当前位置:网站首页>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
边栏推荐
猜你喜欢
dllexport和dllimport
Doris学习笔记之数据表的创建
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
洛谷P4047 [JSOI2010]部落划分 题解
US stock listing of polar: how can the delivery of 55000 units support the valuation of more than 20billion US dollars
How to query the baby category of tmall on Taobao
洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解
tonybot 人形机器人 查看端口并对应端口 0701
Puzzle (016.4) domino effect
使用并行可微模拟加速策略学习
随机推荐
Niuke: crossing the river
7-24 reduction of the simplest fraction (rolling Division)
天谋科技 Timecho 完成近亿元人民币天使轮融资,打造工业物联网原生时序数据库
ConstraintLayout 的使用
Find books ()
7-17 crawling worms (break exercise)
7-3 count the number of words in a line of text
Code writing and playing method of tonybot humanoid robot at fixed distance
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
7-23 currency conversion (using array conversion)
2021-10-16 initial programming
7-28 monkeys choose King (Joseph problem)
dllexport和dllimport
Dllexport and dllimport
Why is this error reported when modifying records in the database
如何查询淘宝天猫的宝贝类目
7-20 print 99 formula table (format output)
Puzzle (016.4) domino effect
洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解
Use of constraintlayout