当前位置:网站首页>Day 05 - file operation function
Day 05 - file operation function
2022-07-01 05:16:00 【Worth the trip -rui】



The first 05 God - File operation related functions
01.stat function ( a key )
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
int stat(const char* path,struct stat *buf)
int lstat(const char *pathname,struct stat *buf)
function :
Get file status information
stat and lstat The difference between :
When the file is a symbolic link ,lstat What is returned is the information of the symbolic link itself ;
and stat Returns information about the file specified by the link
Parameters :
path: file name
buf: The structure that holds the file information
Return value
success :0;
Failure :-1;
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-XdrXSGRQ-1656152623410)(D:\ picture \image-20220625073954397.png)]
Gets file property information
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
int main()
{
int ret=-1;
struct stat s;
ret=stat("txt",&s);
if(ret==-1)
{
perror("stat");
return 1;
}
printf("st_dev=%lu\n",s.st_dev);
printf("st_ino=%ld\n",s.st_ino);
return 0;
}
st_mode Get the first version of file type and permission
man 2 stat manual
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-RwSiqGLs-1656152623411)(D:\ picture \image-20220625083229032.png)]
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
int show_file_type(struct stat *s)
{
switch(s->st_mode&S_IFMT){
case S_IFBLK:printf("block device Block device \n");break;
case S_IFCHR:printf("character device Character device \n");break;
case S_IFDIR:printf("directory Catalog \n");break;
case S_IFIFO:printf("IS A FIFO The Conduit \n");break;
case S_IFLNK:printf("IS A symLINK A symbolic link \n");break;
case S_IFREG:printf("IS A regular file Ordinary documents \n");break;
case S_IFSOCK:printf("IS A SOCKET\n");break;
default :printf("unknown\n");break;
}
return 0;
}
int main(int argc,char *argv[])
{
struct stat s;
if(argc!=2)
{
printf("usage:./a.out filename");
return 1;
}
// Get file information
int ret=-1;
ret=stat(argv[1],&s);
if(ret==-1)
{
perror("stat");
return 1;
}
// Show file type
show_file_type(&s);
return 0;
}
Second version
int show_file_type_v2(struct stat *s)
{
if(S_ISREG(s->st_mode))
{
printf("is a regular file\n");
return 0;
}
if(S_ISLNK(s->st_mode))
{
printf("is a symblic link\n");
return 0;
}
if(S_ISBLK(s->st_mode))
{
printf("is a block device\n");
return 0;
}
if(S_ISDIR(s->st_mode))
printf("is a directory\n");
return 0;
}
if(S_ISCHR(s->st_mode))
{
printf("is a character devicce\n");
return 0;
}
if(S_ISFIFO(s->st_mode))
{
printf("is a FIFO\n");
return 0;
}
if(S_ISSOCK(s->st_mode))
{
printf("is a socket Socket \n");
return 0;
}
return 0;
}
File permissions
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-o7mGUKDC-1656152623413)(D:\ picture \image-20220625093211566.png)]
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-WFyEeQgZ-1656152623414)(D:\ picture \image-20220625093507666.png)]
access function
chmod function
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-9hSaHIpB-1656152623415)(D:\ picture \image-20220625094310470.png)]
chown function
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-vlX51xUl-1656152623416)(D:\ picture \image-20220625094400639.png)]
truncate function
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-gDR1wt97-1656152623416)(D:\ picture \image-20220625094429486.png)]
link function
symlink function
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-kIaLHiRa-1656152623417)(D:\ picture \image-20220625094523851.png)]
readlink function
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-eO9nPkGk-1656152623418)(D:\ picture \image-20220625094601834.png)]
unlink function
rename function
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-h9rrKCYp-1656152623418)(D:\ picture \image-20220625094710678.png)]
02. File descriptor copy
dup function
#include<unistd.h>
int dup(int oldfd);
function :
adopt oldfd Copy out a new file descriptor , The new file descriptor is the smallest available file descriptor in the call process file descriptor table , Final oldfd And the new file descriptor point to the same file
Parameters :
oldfd: File descriptors that need to be copied oldfd
Return value :
success : New file descriptor
Failure :-1;
The book explains :[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-6sBzldeH-1656152623419)(D:\ picture \image-20220625100258289.png)]
Share one file offset
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<unistd.h>
int main()
{
// Open file
int fd=-1;
fd=open("txt",O_RDWR|O_CREAT,0644);
if(fd==-1)
{
perror("open");
return 0;
}
// operation
int newfd=dup(fd);
if(newfd==-1)
{
perror("dup");
return 0;
}
write(fd,"asdfsd",6);
write(newfd,"12456",5);
// Close file
close(fd);
close(newfd);
}
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-hO368tFx-1656152623419)(D:\ picture \image-20220625101913209.png)]
dup2 function
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-n1TLaZdi-1656152623420)(D:\ picture \image-20220625102931102.png)]
dup2 You can directly complete the above operation
The book explains :
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-8k1y0cAo-1656152623421)(D:\ picture \image-20220625103108205.png)]
fcnlt function
Copy file descriptors
#include<unistd.h>
#include<fcntl.h>
int fcntl(int fd,int cmd ,.../* arg*/);
function : Change the nature of open files ,fcntl Provides control over descriptors .
Parameters :
fd: File descriptor for operation
cmd: Mode of operation
arg: in the light of cmd Value ,fcntl Can accept the third parameter int arg.
Return value :
success : Return some other value
Failure :-1
There are five functions :
1) Copy an existing descriptor (cmd=F_DUPFD)
2) get / Set file descriptor flag (cmd=F_GETFD)
3) get / Set file status flag (cmd=F_GETFL or F_SETFL)
4) get / Set asynchronous I/O ownership (cmd=F_GETOWN or FSETOWN)
5) get / Set record lock (cmd=F_GETLK,F_SETLK or F_SETLKW)
The book explains :
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-Zw8RZxFM-1656152623422)(D:\ picture \image-20220625110957038.png)]
getcwd function
#include<unistd.h>
char* getcwd(char *buf,size_t size)
function : Get the current process Directory
Parameters :
buf: Buffer zone , Store the current working directory
size: Buffer size
Return value :
success :buf Save the current process working directory as the location
Failure :NULL
chdir function
#include<unistd.h>
int chdir(const char*path)
function : Modify the current process
Parameters :
path: Switched path
Return value :
success :0
Failure :-1
#include<unistd.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int ret=-1;
char buf[128];
// Get the current path
if(NULL==getcwd(buf,128))
{
perror("getcwd");
return 1;
}
printf("buf:%s",buf);
// Modify the path
ret=chdir("/home/rui");
if(-1==ret)
{
perror("chdir");
return 1;
}
// Get the path
memset(buf,0,128);
if(NULL==getcwd(buf,128))
{
perror("getcwd");
return 1;
}
printf("buf:%s",buf);
return 0;
}
opendir function
closedir function
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-r5UWEZ6i-1656152623422)(D:\ picture \image-20220625175011901.png)]
readdir function
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-uwuddhUr-1656152623422)(D:\ picture \image-20220625181026538.png)]
}
// Get the path
memset(buf,0,128);
if(NULL==getcwd(buf,128))
{
perror("getcwd");
return 1;
}
printf("buf:%s",buf);
return 0;
}
#### opendir function
#### closedir function
[ Outside the chain picture transfer in ...(img-r5UWEZ6i-1656152623422)]
#### readdir function
[ Outside the chain picture transfer in ...(img-uwuddhUr-1656152623422)]
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-FaCCjihx-1656152623423)(D:\ picture \image-20220625181919233.png)]
边栏推荐
- 【暑期每日一題】洛穀 P1568 賽跑
- QDataStream的简单读写验证
- Global and Chinese market of search engine optimization (SEO) software 2022-2028: Research Report on technology, participants, trends, market size and share
- LevelDB源码分析之LRU Cache
- [daily question in summer] Luogu p5740 [deep foundation 7. Example 9] the best student
- Pico Neo3手柄抓取物体
- And search: the suspects (find the number of people related to the nth person)
- 提高企业产品交付效率系列(1)—— 企业应用一键安装和升级
- Software intelligence: the "world" and "boundary" of AI sentient beings in AAAs system
- FileInputStream
猜你喜欢

【暑期每日一题】洛谷 P5886 Hello, 2020!
![Is there any good website or software for learning programming? [introduction to programming]?](/img/ae/68a5880f313c307880ac80bd200530.jpg)
Is there any good website or software for learning programming? [introduction to programming]?

eBPF Cilium实战(2) - 底层网络可观测性

在Rainbond中一键部署高可用 EMQX 集群

How to traverse massive data in redis

提高企业产品交付效率系列(1)—— 企业应用一键安装和升级

智慧运维:基于 BIM 技术的可视化管理系统

Use and principle of wait notify

Leetcode1497- check whether array pairs can be divided by K - array - hash table - count
![[NLP Li Hongyi] notes](/img/8e/a51ca5eee638facd54270fb28d2fce.jpg)
[NLP Li Hongyi] notes
随机推荐
线程类的几大创建方法
Data consistency between redis and database
Global and Chinese market for kitchen range hoods 2022-2028: Research Report on technology, participants, trends, market size and share
AcWing 888. Finding combinatorial number IV (the problem of finding combinatorial number with high precision)
Global and Chinese markets of Ethernet communication modules 2022-2028: Research Report on technology, participants, trends, market size and share
Pytorch neural network construction template
[hard ten treasures] - 1 [basic knowledge] classification of power supply
Thread process foundation of JUC
LeetCode1497-检查数组对是否可以被 k 整除-数组-哈希表-计数
【暑期每日一題】洛穀 P1568 賽跑
[hard ten treasures] - 2 [basic knowledge] characteristics of various topological structures of switching power supply
字符输入流与字符输出流
Leetcode316- remove duplicate letters - stack - greedy - string
使用 Nocalhost 开发 Rainbond 上的微服务应用
Global and Chinese markets for business weather forecasting 2022-2028: Research Report on technology, participants, trends, market size and share
如何选择导电滑环材料
Print stream and system setout();
Copy baby prompt: material cannot be empty. How to solve it?
Usage and principle of synchronized
Vérification simple de la lecture et de l'écriture de qdatastream