当前位置:网站首页>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
边栏推荐
- Thread. Sleep and timeunit SECONDS. The difference between sleep
- Adc128s022 ADC Verilog design and Implementation
- Tiantu investment sprint Hong Kong stocks: asset management scale of 24.9 billion, invested in xiaohongshu and Naixue
- Understand the application scenario and implementation mechanism of differential segment
- Zzuli:1059 highest score
- C language memory function
- pyQt界面制作(登录+跳转页面)
- Sword finger offer 28 Symmetric binary tree
- 光猫超级账号密码、宽带账号密码 获取
- Sendmail can't send mail and it's too slow to send. Solve it
猜你喜欢

Tiantu investment sprint Hong Kong stocks: asset management scale of 24.9 billion, invested in xiaohongshu and Naixue

天谋科技 Timecho 完成近亿元人民币天使轮融资,打造工业物联网原生时序数据库

tonybot 人形机器人 定距移动 代码编写玩法

Pyqt interface production (login + jump page)

Tonybot humanoid robot checks the port and corresponds to port 0701

puzzle(016.3)千丝万缕

To improve efficiency or increase costs, how should developers understand pair programming?

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

Rasterization: a practical implementation (2)

Bucket sorting in C language
随机推荐
洛谷P5194 [USACO05DEC]Scales S 题解
Zzuli:1053 sine function
Programming language: the essence of type system
ShowMeBug入驻腾讯会议,开启专业级技术面试时代
Convert string to decimal integer
Recent learning summary
Doris学习笔记之数据表的创建
Zzuli:1040 sum of sequence 1
String sort
DDK for XP
Zzuli:1041 sum of sequence 2
Accelerating strategy learning using parallel differentiable simulation
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
7-3 count the number of words in a line of text
表单文本框的使用(一) 选择文本
7-16 find the set of integers that meet the given conditions
7-17 crawling worms (break exercise)
Zzuli:1047 logarithmic table
7-4 BCD decryption (10 points)
Output student grades