当前位置:网站首页>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
边栏推荐
- Zzuli:1049 square sum and cubic sum
- On MEM series functions of C language
- Programming language: the essence of type system
- Luogu p5536 [xr-3] core city solution
- 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
- Add ZABBIX calculation type itemcalculated items
- Etcd cluster permission management and account password usage
- Zzuli:1055 rabbit reproduction
- 【7.3】146. LRU caching mechanism
- Time conversion ()
猜你喜欢

如何查询淘宝天猫的宝贝类目

retrofit

Pyqt interface production (login + jump page)

MySQL multi table query subquery

puzzle(016.4)多米诺效应

Code writing and playing method of tonybot humanoid robot at fixed distance

dllexport和dllimport

Why is this error reported when modifying records in the database

使用并行可微模拟加速策略学习

分布式事务(Seata) 四大模式详解
随机推荐
7-16 find the set of integers that meet the given conditions
Output student grades
Add ZABBIX calculation type itemcalculated items
Tiantu investment sprint Hong Kong stocks: asset management scale of 24.9 billion, invested in xiaohongshu and Naixue
Tonybot humanoid robot infrared remote control play 0630
7-3 rental (20 points)
Preliminary summary of structure
Pyqt interface production (login + jump page)
Common shortcut keys in PCB
Sub GHz wireless solution Z-Wave 800 Series zg23 SOC and zgm230s modules
1017 a divided by B (20 points)
Get permissions dynamically
Tailing rushes to the scientific and Technological Innovation Board: it plans to raise 1.3 billion, and Xiaomi Changjiang is the shareholder
Zzuli:1046 product of odd numbers
Programming language: the essence of type system
J-luggage lock of ICPC Shenyang station in 2021 regional games (simple code)
retrofit
Use of form text box (I) select text
Common commands for getting started with mongodb database
[combinatorics] permutation and combination (set combination, one-to-one correspondence model analysis example)