当前位置:网站首页>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)]
边栏推荐
- 【暑期每日一题】洛谷 P5886 Hello, 2020!
- Use and principle of wait notify
- QDataStream的簡單讀寫驗證
- 线程安全问题
- Global and Chinese markets of InGaAs APD arrays 2022-2028: Research Report on technology, participants, trends, market size and share
- 【暑期每日一题】洛谷 P2026 求一次函数解析式
- 1076 Forwards on Weibo
- Vérification simple de la lecture et de l'écriture de qdatastream
- And search: the suspects (find the number of people related to the nth person)
- CockroachDB 分布式事务源码分析之 TxnCoordSender
猜你喜欢

复制宝贝提示材质不能为空,如何解决?

Mathematical knowledge: finding the number of divisors

液压滑环的特点讲解
![Solution: thread 1:[< *> setvalue:forundefined key]: this class is not key value coding compliant for the key*](/img/88/0b99d1db2cdc70ab72d2b3c623dfaa.jpg)
Solution: thread 1:[< *> setvalue:forundefined key]: this class is not key value coding compliant for the key*

LevelDB源码分析之memtable

el-cascader回显失败;el-cascader回显不出来

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

Use of STM32 expansion board temperature sensor and temperature humidity sensor

Explanation of characteristics of hydraulic slip ring

Unity drags and modifies scene camera parameters under the editor
随机推荐
Oracle views the creation time of the tablespace in the database
Design experience of Meizhou clinical laboratory
Precautions for use of conductive slip ring
AcWing 887. Finding combinatorial number III (Lucas theorem)
Rainbond结合NeuVector实践容器安全管理
Copier le matériel de conseils de bébé ne peut pas être vide, comment résoudre?
【暑期每日一题】洛谷 P1568 赛跑
Use and principle of Park unpark
Design and application of immutable classes
Pytorch neural network construction template
Is there any good website or software for learning programming? [introduction to programming]?
线程安全问题
AcWing 884. Gauss elimination for solving XOR linear equations
积分商城游戏能够给商家带来什么?怎么搭建积分商城?
LevelDB源码分析之memtable
Pytoch (I) -- basic grammar
[daily question in summer] Luogu p2026 find the analytic formula of primary function
[daily question in summer] Luogu p7222 [rc-04] informatics competition
Unity drags and modifies scene camera parameters under the editor
AcWing 889. 01 sequence satisfying the condition (Cartland number)