当前位置:网站首页>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
边栏推荐
- Selective sorting
- Zzuli:1041 sum of sequence 2
- Zzuli:1040 sum of sequence 1
- Zzuli:1048 factorial table
- Sword finger offer 28 Symmetric binary tree
- Common shortcut keys in PCB
- Dllexport et dllimport
- [qingniaochangping campus of Peking University] in the Internet industry, which positions are more popular as they get older?
- Solr series of full-text search engines - basic principles of full-text search
- Time conversion ()
猜你喜欢

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

【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?
![Luogu p4047 [jsoi2010] tribal division solution](/img/7f/3fab3e94abef3da1f5652db35361df.png)
Luogu p4047 [jsoi2010] tribal division solution

dllexport和dllimport

Zzuli:1053 sine function

Bucket sorting in C language

tonybot 人形机器人 查看端口并对应端口 0701

Tonybot humanoid robot checks the port and corresponds to port 0701

Rasterization: a practical implementation (2)

Detailed explanation of four modes of distributed transaction (Seata)
随机推荐
How Facebook moves instagram from AWS to its own server
Zzuli:1055 rabbit reproduction
NPM install is stuck with various strange errors of node NPY
洛谷P5194 [USACO05DEC]Scales S 题解
2021-10-16 initial programming
Protobuf and grpc
Zhonggan micro sprint technology innovation board: annual revenue of 240million, net loss of 17.82 million, proposed to raise 600million
Detailed explanation of four modes of distributed transaction (Seata)
Luogu p4047 [jsoi2010] tribal division solution
Zzuli:1042 sum of sequence 3
tonybot 人形机器人 定距移动 代码编写玩法
NOI OPENJUDGE 1.5(23)
J-luggage lock of ICPC Shenyang station in 2021 regional games (simple code)
Luogu p5194 [usaco05dec]scales s solution
洛谷P3065 [USACO12DEC]First! G 题解
Dllexport et dllimport
Mongodb index
On MEM series functions of C language
Common shortcut keys in PCB
Zzuli:1041 sum of sequence 2