Yuchuan_linux_C 编程之八 文件操作相关函数
一、整体大纲

st_mode整体介绍:

st_mode详细介绍:

二、 Linux文件操作相关函数
1. stat
- 作用:获得文件信息,也可以获取文件大小。
- 头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
- 函数原型
int stat(const char *path, struct stat *buf);
int fstat(int fd, struct stat *buf);
int lstat(const char *path, struct stat *buf);
- 参数说明:
path文件名
buf传出参数,定义结构体struct stat sb; &sb
- 返回值
失败:返回-1,设置errno
成功:返回0
注意: stat碰到链接,会追溯到源文件,穿透!!!lstat并不会穿透。
stat结构体:
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for file system I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
struct stat
linux命令stat执行结果:
[root@centos linuxC]# stat stat.c
文件:"stat.c"
大小: 块: IO 块: 普通文件
设备:fd00h/64768d Inode: 硬链接:
权限:(/-rw-r--r--) Uid:( / root) Gid:( / root)
环境:unconfined_u:object_r:usr_t:s0
最近访问:-- ::15.149083960 +
最近更改:-- ::15.149083960 +
最近改动:-- ::15.202084912 +
注意三个时间的区别
time_t st_atime; /* time of last access */ 文件被读,比如cat,open读等
time_t st_mtime; /* time of last modification */ 文件内容发生改变
time_t st_ctime; /* time of last status change */ 文件属性发生变化,比如大小,权限,硬连接数等

上图的解释:
- 其他用户权限
- 组用户权限
- 用户权限
- 特殊权限位
- 文件类型
示例:
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h> int
main(int argc, char *argv[])
{
struct stat sb; if (argc != ) {
fprintf(stderr, "Usage: %s <pathname>\n", argv[]);
exit(EXIT_FAILURE);
} if (stat(argv[], &sb) == -) {
perror("stat");
exit(EXIT_FAILURE);
} printf("File type: "); switch (sb.st_mode & S_IFMT) {
case S_IFBLK: printf("block device\n"); break;
case S_IFCHR: printf("character device\n"); break;
case S_IFDIR: printf("directory\n"); break;
case S_IFIFO: printf("FIFO/pipe\n"); break;
case S_IFLNK: printf("symlink\n"); break;
case S_IFREG: printf("regular file\n"); break;
case S_IFSOCK: printf("socket\n"); break;
default: printf("unknown?\n"); break;
}
printf("I-node number: %ld\n", (long) sb.st_ino); printf("Mode: %lo (octal)\n",
(unsigned long) sb.st_mode); printf("Link count: %ld\n", (long) sb.st_nlink);
printf("Ownership: UID=%ld GID=%ld\n",
(long) sb.st_uid, (long) sb.st_gid); printf("Preferred I/O block size: %ld bytes\n",
(long) sb.st_blksize);
printf("File size: %lld bytes\n",
(long long) sb.st_size);
printf("Blocks allocated: %lld\n",
(long long) sb.st_blocks); printf("Last status change: %s", ctime(&sb.st_ctime));
printf("Last file access: %s", ctime(&sb.st_atime));
printf("Last file modification: %s", ctime(&sb.st_mtime)); exit(EXIT_SUCCESS);
} Linux自带示例(打印文件相关信息)
Linux自带示例(打印文件相关信息)
需求:使用stat实现实现 ls -l 的功能?如下所示
[root@centos linuxC]# ll -l xx.log
-rw-r--r--. root root 4月 : xx.log
在实现的过程中需要获取用户名及组名,因此先看两个函数:
1)getpwuid
- 作用:通过用户的uid获取用户名
- 头文件
#include <sys/types.h>
#include <pwd.h>
- 函数原型
struct passwd *getpwuid(uid_t uid);
- 参数说明:
uid用户的uid
- 返回值
失败:返回NULL
成功:返回 struct passwd * 结构体指针
其中:
struct passwd {
char *pw_name; /* username */ 用户名
char *pw_passwd; /* user password */
uid_t pw_uid; /* user ID */
gid_t pw_gid; /* group ID */
char *pw_gecos; /* user information */
char *pw_dir; /* home directory */
char *pw_shell; /* shell program */
};
struct passwd
2)getgrgid
- 作用:通过用户的gid获取用户组名
- 头文件
#include <sys/types.h>
#include <grp.h>
- 函数原型
struct group *getgrgid(gid_t gid);
- 参数说明:
gid用户组的gid
- 返回值
失败:返回NULL
成功:返回 struct group * 结构体指针
其中:
struct group {
char *gr_name; /* group name */
char *gr_passwd; /* group password */
gid_t gr_gid; /* group ID */
char **gr_mem; /* group members */
};
struct group
3)localtime
- 作用:获取本地时间
- 头文件
#include <time.h>
- 函数原型
struct tm *localtime(const time_t *timep);
- 参数说明:
timep:一个时间相关的结构体
- 返回值
失败:返回NULL
成功:返回 struct tm * 结构体指针
其中:
struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* day of the month */
int tm_mon; /* month */
int tm_year; /* year */
int tm_wday; /* day of the week */
int tm_yday; /* day in the year */
int tm_isdst; /* daylight saving time */
};
struct tm
传入参数 timep 对应stat函数得到的结构体的秒数(time_t类型)。
最终实现:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<time.h>
#include <grp.h>
#include <pwd.h> int main(int argc, char *argv[])
{
if (argc != )
{
printf("./a.out filename\n");
return -;
}
struct stat sb;
stat(argv[], &sb); char stmode[] = {};
memset(stmode, '-', sizeof(stmode)-); //解析文件属性
if (S_ISREG(sb.st_mode)) stmode[] = '-'; //普通文件
if (S_ISDIR(sb.st_mode)) stmode[] = 'd';
if (S_ISCHR(sb.st_mode)) stmode[] = 'c';
if (S_ISBLK(sb.st_mode)) stmode[] = 'b';
if (S_ISFIFO(sb.st_mode)) stmode[] = 'p';
if (S_ISLNK(sb.st_mode)) stmode[] = 'l';
if (S_ISSOCK(sb.st_mode)) stmode[] = 's'; //解析权限
//user
if (sb.st_mode & S_IRUSR) stmode[] = 'r';
if (sb.st_mode & S_IWUSR) stmode[] = 'w';
if (sb.st_mode & S_IXUSR) stmode[] = 'x';
//group
if (sb.st_mode & S_IRGRP) stmode[] = 'r';
if (sb.st_mode & S_IWGRP) stmode[] = 'w';
if (sb.st_mode & S_IXGRP) stmode[] = 'x';
//other
if (sb.st_mode & S_IROTH) stmode[] = 'r';
if (sb.st_mode & S_IWOTH) stmode[] = 'w';
if (sb.st_mode & S_IXOTH) stmode[] = 'x'; //分析 用户名,组名可以通过函数获得 getpwuid, getgrgid
//时间获取
struct tm *filetm = localtime(&sb.st_atim.tv_sec);
char timebuf[] = {};
sprintf(timebuf, "%d月 %d %02d:%02d", filetm->tm_mon+, filetm->tm_mday, filetm->tm_hour, filetm->tm_min); printf("%s %ld %s %s %ld %s %s\n", stmode, sb.st_nlink, getpwuid(sb.st_uid)->pw_name,
getgrgid(sb.st_gid)->gr_name, sb.st_size, timebuf, argv[]); return ;
} 使用stat实现一个ls -l命令
使用stat实现一个ls -l命令
2. access
- 作用:测试指定文件是否有某种权限
- 头文件
#include <unistd.h>
- 函数原型
int access(const char *pathname, int mode);
- 参数说明:
pathname文件名
mode:
R_OK
W_OK
X_OK
F_OK
- 返回值
失败:返回-1,设置errno
成功:如果有权限或者文件存在,对应返回0
#include<stdio.h>
#include <unistd.h> int main(int argc, char *argv[])
{
if (argc != )
{
printf("./a.out filename\n");
return -;
}
if (access(argv[], R_OK) == ) printf("%s read ok!\n", argv[]);
if (access(argv[], W_OK) == ) printf("%s write ok!\n", argv[]);
if (access(argv[], X_OK) == ) printf("%s exe ok!\n", argv[]);
if (access(argv[], F_OK) == ) printf("%s file exists!\n", argv[]); return ; } 判断文件读写执行及文件是否存在
判断文件读写执行及文件是否存在
3. chmod
#include <sys/stat.h>
int chmod(const char *path, mode_t mode);
4. truncate
- 函数作用:截断文件
- 头文件
#include <unistd.h>
#include <sys/types.h>
- 函数原型
int truncate(const char *path, off_t length);
int ftruncate(int fd, off_t length);
- 参数说明:
path文件名
length长度,长度如果大于原文件,直接拓展,如果小于原文件,截断为length长度。
- 返回值
成功:返回0
失败:返回-1,设置errno
示例:
#include<stdio.h>
#include<sys/types.h>
#include<stdio.h> int main(int argc, char *argv[])
{
if (argc != )
{
printf("./a.out filename\n");
return -;
}
truncate(argv[], );
return ;
} truncate示例
truncate示例
5. link
- 函数作用:创建硬连接
- 头文件
#include <unistd.h>
- 函数原型
int link(const char *oldpath, const char *newpath);
- 参数说明:
oldpath原文件
newpath硬连接文件
- 返回值
成功:返回0
失败:返回-1,设置errno
示例:
#include<stdio.h>
#include <unistd.h> int main(int argc, char *argv[])
{
if (argc != )
{
printf("./a.out filename\n");
return -;
} char filename[] = {};
sprintf(filename, "%s_hard", argv[]); link(argv[], filename); return ;
} 创建硬连接示例
创建硬连接示例
6. symlink
- 函数作用:创建软连接
- 头文件
#include <unistd.h>
- 函数原型
int symlink(const char *oldpath, const char *newpath);
- 参数解释:
oldpath原文件
newpath创建软连接文件
- 返回值
成功:返回0
失败:返回-1,设置errno
示例:
#include<stdio.h>
#include <unistd.h> int main(int argc, char *argv[])
{
if (argc != )
{
printf("./a.out filename\n");
return -;
} char filename[] = {};
sprintf(filename, "%s_soft", argv[]); symlink(argv[], filename); return ;
} 创建软连接
创建软连接
6. readlink
- 函数作用:读取文件链接信息
- 头文件
#include <unistd.h>
- 函数原型
ssize_t readlink(const char *path, char *buf, size_t bufsiz);
- 参数解释:
path链接名
buf缓冲区
bufsiz缓冲区大小
- 返回值
成功:返回buf填充的大小
失败:返回-1,设置errno
示例:
#include<stdio.h>
#include <unistd.h> int main(int argc, char *argv[])
{
if (argc != )
{
printf("./a.out filename\n");
return -;
} char buf[] = {};
readlink(argv[], buf, sizeof(buf));
printf("buf is %s\n", buf); unlink(argv[]); return ;
} readlink示例
readlink示例
7. unlink
- 函数作用:删除软硬链接
- 头文件
#include <unistd.h>
- 函数原型
int unlink(const char *pathname);
- 函数参数:
pathname 链接名,文件也可以
- 返回值
成功:返回0
失败:返回-1,设置errno
示例:
#include<stdio.h>
#include <unistd.h>
#include<stdlib.h>
#include<fcntl.h>
#include<string.h>
#include<sys/types.h> int main(int argc, char *argv[])
{
if (argc != )
{
printf("./a.out filename\n");
return -;
} int fd = open(argv[], O_WRONLY|O_CREAT, );
//注意只要有进程在使用该文件,则unlink在该文件退出时删除该文件
unlink(argv[]); int ret = write(fd, "hello", );
if (ret > )
{
printf("write ok! %d\n", ret);
}
if (ret < )
{
perror("write err");
} close(fd); return ;
} unlink示例
unlink示例
8. chown
- 函数作用:修改文件属主及属组
- 头文件
#include <unistd.h>
- 函数原型
int chown(const char *path, uid_t owner, gid_t group);
- 函数参数:
path文件名
owner用户ID,/etc/passwd
owner组ID,/etc/group
- 返回值
成功:返回0
失败:返回-1,设置errno
9. rename
- 函数作用:文件或者目录重命名
- 头文件
#include <stdio.h>
- 函数原型
int rename(const char *oldpath, const char *newpath);
- 参数说明:
oldpath文件名
newpath文件新名
- 返回值
成功:返回0
失败:返回-1,设置errno
示例:
#include<stdio.h> int main(int argc, char *argv[])
{
if (argc != )
{
printf("./a.out filename\n");
return -;
} char buf[] = {};
sprintf(buf, "%s_new", argv[]);
rename(argv[], buf); return ;
} rename示例
rename示例
Yuchuan_linux_C 编程之八 文件操作相关函数的更多相关文章
- C语言文件操作相关函数
在实际应用中,我们往往需要对文件进行操作,下面我将介绍C语言的一些关于操作文件的函数. 一.计算机文件 计算机文件是以计算机硬盘为载体存储在计算机上的信息集合,是存储在某种长期储存设备上的一段数据流. ...
- (安全之路)从头开始学python编程之文件操作
0x00 python学习路径 b站(哔哩哔哩)视频,w3cschool(详情百度),官方文档,各大群内获取资料等等方式 0x01 python的学习要点 open()函数:有两个参数,文件名跟模式, ...
- linux编程之文件操作
在linux下用文件描述符来表示设备文件盒普通文件,文件描述符是一个整型的数据,所有对文件的操作都是通过文件描述符来实现的. 文件描述符是文件系统中连接用户空间和内核空间的枢纽,当我们打开一个或者创建 ...
- Java IO编程——File文件操作类
在Java语言里面提供有对于文件操作系统操作的支持,而这个支持就在java.io.File类中进行了定义,也就是说在整个java.io包里面,File类是唯一 一个与文件本身操作(创建.删除.重命名等 ...
- 文件操作相关函数(POSIX 标准 open,read,write,lseek,close)
POSIX标准 open函数属于Linux中系统IO,用于“打开”文件,代码打开一个文件意味着获得了这个文件的访问句柄. int fd = open(参数1,参数2,参数3): int fd = op ...
- java学习笔记之IO编程—File文件操作类
1. File类说明 在Java语言里面提供有对于文件操作系统操作的支持,而这个支持就在java.io.File类中进行了定义,也就是说在整个java.io包里面,File类是唯一一个与文件本身操作( ...
- POJ C++程序设计 编程题#3 编程作业—文件操作与模板
编程题#3: 整数的输出格式 来源: POJ(Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 1000kB 描述 利 ...
- POJ C++程序设计 编程题#2 编程作业—文件操作与模板
编程题#2: 实数的输出格式 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 1000kB 描述 ...
- POJ C++程序设计 编程题#1 编程作业—文件操作与模板
编程题#1 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 实现一个三维数组 ...
随机推荐
- 代码审计中的CSRF
0x00 背景 CSRF漏洞中文名为“跨站请求伪造”,英文别名为“one-click-attack”.从字面上我们就可以看出,这是一种劫持其他用户进行非法请求的攻击方式,主要用于越权操作,与XSS相比 ...
- Codeforces Round #572 (Div. 1) 差E
Codeforces Round #572 (Div. 1) A2 题意:给一棵树,带边权,互不相同且为偶数.每次操作是选两个叶子然后在路径上同时加一个数.初始边权全是0,求一个操作序列使得最终边权与 ...
- 一、SpringBoot学习笔记_Eclipse 安装 SpringBoot、配置Gradle
首先查看Eclipse 的版本 点击Help ,然后在点击About 就会出现下面的图片 去官网下载对应版本的SpringBoot插件压缩包,下载保存到能找到的位置 然后 点击 Help Inst ...
- Android内存调优的一些方法
一.巧妙使用软引用和弱引用 软引用,系统内存紧张时会回收软引用对象,一般用用高速缓存,例如图片缓存,我们一般通过内存缓存图片来提高图片加载速度,但内存吃紧的时候可以通过软引用及时对图片资源回收. 弱引 ...
- springboot学习笔记:6.内置tomcat启动和外部tomcat部署总结
springboot的web项目的启动主要分为: 一.使用内置tomcat启动 启动方式: 1.IDEA中main函数启动 2.mvn springboot-run 命令 3.java -jar XX ...
- [洛谷P3806] [模板] 点分治1
洛谷 P3806 传送门 这个点分治都不用减掉子树里的了,直接搞就行了. 注意第63行 if(qu[k]>=buf[j]) 不能不写,也不能写成>. 因为这个WA了半天...... 如果m ...
- htaccess 伪静态的规则
利用htaccess文件可以很好的进行站点伪静态,并且形成的目标地址与真正的静态页面几乎一模一样,如abc.html等,伪静态可以非常好的结合SEO来提高站点的排名,并且也能给人一种稳定的印象. 由于 ...
- node-sass 安装失败的各种坑
开始的时候引入别人的一个项目 npm install npm run dev 启动项目 报错 > node build/dev-server.js Listening at http://loc ...
- textarea 是否换行的问题解决
需求:判断当前textarea是否已经换行(这个换行有2种方式:1.不断输入文字直到超过指定宽度后自动换行:2.按了回车以后进行换行) 单纯的解决第二种换行很简单.网上提供了很多常规的解决方案. De ...
- less 的使用方法总结
一. 安装和使用 LESS 1.1 安装 使用命令行安装 LESS npm install -g less 1.2 使用 less 有多种的使用方法,在这里我向大家介绍最常用的俩种方法. 第一种是直接 ...