当前位置:网站首页>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
边栏推荐
- US stock listing of polar: how can the delivery of 55000 units support the valuation of more than 20billion US dollars
- 7-20 print 99 formula table (format output)
- NOI OPENJUDGE 1.3(06)
- Zzuli:1047 logarithmic table
- Zzuli:1040 sum of sequence 1
- [combinatorics] permutation and combination (set combination, one-to-one correspondence model analysis example)
- 如何查询淘宝天猫的宝贝类目
- 7-6 mixed type data format input
- C language STR function
- 7-14 sum integer segments (C language)
猜你喜欢

Analysis of gene family characteristics - chromosome location analysis

Niuke: crossing the river

Frequently asked questions: PHP LDAP_ add(): Add: Undefined attribute type in

On MEM series functions of C language

Understand the application scenario and implementation mechanism of differential segment

C language to realize mine sweeping
![洛谷P4047 [JSOI2010]部落划分 题解](/img/7f/3fab3e94abef3da1f5652db35361df.png)
洛谷P4047 [JSOI2010]部落划分 题解

US stock listing of polar: how can the delivery of 55000 units support the valuation of more than 20billion US dollars

MySQL multi table query subquery

Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
随机推荐
Find the sum of the elements of each row of the matrix
String substitution
Zzuli:1052 sum of sequence 4
Puzzle (016.3) is inextricably linked
ShowMeBug入驻腾讯会议,开启专业级技术面试时代
puzzle(016.4)多米诺效应
FPGA blocking assignment and non blocking assignment
Zzuli:1040 sum of sequence 1
pyQt界面制作(登录+跳转页面)
7-3 rental (20 points)
Luogu p5194 [usaco05dec]scales s solution
基因家族特征分析 - 染色体定位分析
一文了解微分段应用场景与实现机制
retrofit
Tonybot humanoid robot checks the port and corresponds to port 0701
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
动态获取权限
C language to realize mine sweeping
Niuke: crossing the river
US stock listing of polar: how can the delivery of 55000 units support the valuation of more than 20billion US dollars