当前位置:网站首页>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
边栏推荐
- Dllexport and dllimport
- SSH access control, blocking the IP when logging in repeatedly to prevent brute force cracking
- Zzuli:1042 sum of sequence 3
- Luogu p4047 [jsoi2010] tribal division solution
- C language memory function
- Frequently asked questions: PHP LDAP_ add(): Add: Undefined attribute type in
- Add ZABBIX calculation type itemcalculated items
- Zzuli:1041 sum of sequence 2
- 【7.3】146. LRU caching mechanism
- 提高效率 Or 增加成本,开发人员应如何理解结对编程?
猜你喜欢
Dllexport et dllimport
retrofit
【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?
Pyqt interface production (login + jump page)
Luogu p4047 [jsoi2010] tribal division solution
ShowMeBug入驻腾讯会议,开启专业级技术面试时代
提高效率 Or 增加成本,开发人员应如何理解结对编程?
Bucket sorting in C language
Paper sharing: generating playful palettes from images
Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
随机推荐
7-22 tortoise and rabbit race (result oriented)
Sub GHz wireless solution Z-Wave 800 Series zg23 SOC and zgm230s modules
String reverse order
Zzuli:1058 solving inequalities
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
7-4 BCD decryption (10 points)
Zzuli:1047 logarithmic table
Accelerating strategy learning using parallel differentiable simulation
Etcd cluster permission management and account password usage
Zzuli:1056 lucky numbers
洛谷P4047 [JSOI2010]部落划分 题解
Showmebug entered Tencent conference, opening the era of professional technical interview
7-19 check denomination (solve binary linear equation)
Luogu p5194 [usaco05dec]scales s solution
US stock listing of polar: how can the delivery of 55000 units support the valuation of more than 20billion US dollars
Analysis of gene family characteristics - chromosome location analysis
MySQL multi table query subquery
C language to realize mine sweeping
Frequently asked questions: PHP LDAP_ add(): Add: Undefined attribute type in
数学常数表 by q779