当前位置:网站首页>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)]
边栏推荐
- Pytorch neural network construction template
- 云原生存储解决方案Rook-Ceph与Rainbond结合的实践
- 解决:拖动xib控件到代码文件中,报错setValue:forUndefinedKey:this class is not key value coding-compliant for the key
- And search: the suspects (find the number of people related to the nth person)
- [daily question in summer] Luogu p1568 race
- Go learning notes (5) basic types and declarations (4)
- Solution: drag the Xib control to the code file, and an error setvalue:forundefined key:this class is not key value coding compliant for the key is reported
- Neural networks - use sequential to build neural networks
- Like cloud functions
- eBPF Cilium实战(2) - 底层网络可观测性
猜你喜欢

And search: the suspects (find the number of people related to the nth person)

How to meet the requirements of source code confidentiality and source code security management

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

Mathematical knowledge: finding the number of divisors

STM32 expansion board digital tube display

Tcp/ip explanation (version 2) notes / 3 link layer / 3.2 Ethernet and IEEE 802 lan/man standards

Use of STM32 expansion board temperature sensor and temperature humidity sensor

LeetCode316-去除重复字母-栈-贪心-字符串

解决:拖动xib控件到代码文件中,报错setValue:forUndefinedKey:this class is not key value coding-compliant for the key

LevelDB源码分析之memtable
随机推荐
How to meet the requirements of source code confidentiality and source code security management
RuntimeError: mean(): input dtype should be either floating point or complex dtypes. Got Long instead
Global and Chinese market of 3D design and modeling software 2022-2028: Research Report on technology, participants, trends, market size and share
Design experience of Meizhou clinical laboratory
Serialization and deserialization of objects
[NLP Li Hongyi] notes
Thread process foundation of JUC
Tcp/ip explanation (version 2) notes / 3 link layer / 3.2 Ethernet and IEEE 802 lan/man standards
CockroachDB 分布式事务源码分析之 TxnCoordSender
Query long transaction
Distributed architecture system splitting principles, requirements and microservice splitting steps
FileInputStream
Receiving package install and uninstall events
Leetcode316- remove duplicate letters - stack - greedy - string
[daily question in summer] letter delivery by p1629 postman in Luogu (to be continued...)
eBPF Cilium实战(2) - 底层网络可观测性
Pytoch (IV) -- visual tool visdom
Global and Chinese market for kitchen range hoods 2022-2028: Research Report on technology, participants, trends, market size and share
Pytorch neural network construction template
【暑期每日一题】洛谷 P5886 Hello, 2020!