当前位置:网站首页>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)]
边栏推荐
- C# wpf 使用DockPanel实现截屏框
- LeetCode522-最长特殊序列II-哈希表-字符串-双指针
- LevelDB源码分析之LRU Cache
- CockroachDB: The Resilient Geo-Distributed SQL Database 论文阅读笔记
- FileInputStream
- How to select conductive slip ring material
- AcWing 887. Finding combinatorial number III (Lucas theorem)
- Leetcode1497- check whether array pairs can be divided by K - array - hash table - count
- QDataStream的簡單讀寫驗證
- 1076 Forwards on Weibo
猜你喜欢

Data consistency between redis and database

LevelDB源码分析之memtable

Neural networks - use sequential to build neural networks

Leetcode316- remove duplicate letters - stack - greedy - string
![[data recovery in North Asia] a data recovery case of raid crash caused by hard disk drop during data synchronization of hot spare disk of RAID5 disk array](/img/22/606ff1e8dad3d5896b32d2146b0477.jpg)
[data recovery in North Asia] a data recovery case of raid crash caused by hard disk drop during data synchronization of hot spare disk of RAID5 disk array

Usage and principle of synchronized

Neural network convolution layer

Use and modification of prior network model
![[summer daily question] Luogu p5886 Hello, 2020!](/img/ac/4be05f80aab7fb766674e6e2d16fbc.png)
[summer daily question] Luogu p5886 Hello, 2020!

液压滑环的特点讲解
随机推荐
Unity drags and modifies scene camera parameters under the editor
[daily question in summer] Luogu p5740 [deep foundation 7. Example 9] the best student
Use and principle of wait notify
【暑期每日一题】洛谷 P5740【深基7.例9】最厉害的学生
AssertionError assert I.ndim == 4 and I.shape[1] == 3
Usage and principle of synchronized
导电滑环使用的注意事项
提高企业产品交付效率系列(1)—— 企业应用一键安装和升级
JS random verification code
And search: the suspects (find the number of people related to the nth person)
Global and Chinese markets of InGaAs APD arrays 2022-2028: Research Report on technology, participants, trends, market size and share
How to select conductive slip ring material
Global and Chinese market of broadband amplifiers 2022-2028: Research Report on technology, participants, trends, market size and share
HCIP Day13
Manually implement a simple stack
点赞的云函数
Actual combat: gateway api-2022.2.13
Vmware workstation network card settings and three common network modes
[daily question in summer] Luogu p2026 find the analytic formula of primary function
Principle, technology and implementation scheme of data consistency in distributed database