UNIX环境高级编程——文件和目录
一、获取文件/目录的属性信息
- int stat(const char *path, struct stat *buf);
- int fstat(int fd, struct stat *buf);
- int lstat(const char *path, struct stat *buf);
- 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 */
- };
示例程序:
- #include<sys/types.h>
- #include<sys/stat.h>
- #include<unistd.h>
- #include<fcntl.h>
- #include<stdio.h>
- #include<stdlib.h>
- #include<errno.h>
- #include<string.h>
- #define ERR_EXIT(m) \
- do { \
- perror(m); \
- exit(EXIT_FAILURE); \
- } while(0)
- #define MAJOR(a) (int)((unsigned short)a >> 8) // 高8位,主设备号
- #define MINOR(a) (int)((unsigned short)a & 0xFF)
- int filetype(struct stat *buf)
- {
- int flag = 0;
- printf("Filetype:");
- mode_t mode;
- mode = buf->st_mode;
- switch (mode & S_IFMT)
- {
- case S_IFSOCK:
- printf("socket\n");
- break;
- case S_IFLNK:
- printf("symbolic link\n");
- break;
- case S_IFREG:
- printf("regular file\n");
- break;
- case S_IFBLK:
- printf("block device\n");
- flag = 1;
- break;
- case S_IFDIR:
- printf("directory\n");
- break;
- case S_IFCHR:
- printf("character device\n");
- flag = 1;
- break;
- case S_IFIFO:
- printf("FIFO\n");
- break;
- default:
- printf("unknown file type\n");
- break;
- }
- return flag;
- }
- void fileperm(struct stat *buf, char perm[])
- {
- strcpy(perm, "----------");
- perm[0] = '?';
- mode_t mode;
- mode = buf->st_mode;
- switch (mode & S_IFMT)
- {
- case S_IFSOCK:
- perm[0] = 's';
- break;
- case S_IFLNK:
- perm[0] = 'l';
- break;
- case S_IFREG:
- perm[0] = '-';
- break;
- case S_IFBLK:
- perm[0] = 'b';
- break;
- case S_IFDIR:
- perm[0] = 'd';
- break;
- case S_IFCHR:
- perm[0] = 'c';
- break;
- case S_IFIFO:
- perm[0] = 'p';
- break;
- }
- if (mode & S_IRUSR)
- perm[1] = 'r';
- if (mode & S_IWUSR)
- perm[2] = 'w';
- if (mode & S_IXUSR)
- perm[3] = 'x';
- if (mode & S_IRGRP)
- perm[4] = 'r';
- if (mode & S_IWGRP)
- perm[5] = 'w';
- if (mode & S_IXGRP)
- perm[6] = 'x';
- if (mode & S_IROTH)
- perm[7] = 'r';
- if (mode & S_IWOTH)
- perm[8] = 'w';
- if (mode & S_IXOTH)
- perm[9] = 'x';
- perm[10] = '\0';
- }
- int main(int argc, char *argv[])
- {
- if (argc != 2)
- {
- fprintf(stderr, "Usage %s file\n", argv[0]);
- exit(EXIT_FAILURE);
- }
- printf("Filename:%s\n", argv[1]);
- struct stat sbuf;
- if (lstat(argv[1], &sbuf) == -1)
- ERR_EXIT("stat error");
- printf("file in Dev number:major %d, minor %d\n",
- MAJOR(sbuf.st_dev), MINOR(sbuf.st_dev));
- printf("File inode:%d\n", (int) sbuf.st_ino);
- if (filetype(&sbuf))
- {
- printf("Device number:major %d, minor %d\n",
- MAJOR(sbuf.st_rdev), MINOR(sbuf.st_rdev));
- }
- char perm[11] = {0};
- fileperm(&sbuf, perm);
- printf("File permission bits=%o %s\n", sbuf.st_mode & 07777, perm);
- return 0;
- }
测试如下:
- huangcheng@ubuntu:~$ ./a.out hc
- Filename:hc
- file in Dev number:major 8, minor 1
- File inode:929485
- Filetype:regular file
- File permission bits=644 -rw-r--r--
二、目录的访问
功能说明:打开一个目录
原型:DIR* opendir(char *pathname);
返回值:
打开成功,返回一个目录指针
打开失败,则返回NULL
功能说明:访问指定目录中下一个连接的细节
原型:struct dirent* readdir(DIR *dirptr);
返回值:
返回一个指向dirent结构的指针,它包含指定目录中下一个连接的细节;
没有更多连接时,返回NULL
功能说明:关闭一个已经打开的目录
原型:int closedir (DIR *dirptr);
返回值:调用成功返回0,失败返回-1
- struct dirent
- {
- ino_t d_ino; /* inode number */
- off_t d_off; /* offset to the next dirent */
- unsigned short d_reclen; /* length of this record */
- unsigned char d_type; /* type of file; not supported
- by all file system types */
- char d_name[256]; /* filename */
- };
三、目录的创建删除和权限设置
功能说明:用来创建一个称为pathname的新目录,它的权限位设置为mode
原型:int mkdir(char *pathname,mode_t mode);
返回值:调用成功返回0,失败返回-1
功能说明:删除一个空目录
原型:int rmdir(char *pathname);
返回值:调用成功返回0,失败返回-1
功能说明:用来改变给定路径名pathname的文件的权限位。为了改变一个文件的权限位,进程的有效用户ID必须等于文件的所有者ID,或者该进程必须具有超级用户的权限。
原型:int chmod (char *pathname, mode_t mode);
int fchmod (int fd, mode_t mode);
返回值:调用成功返回0,失败返回-1
功能说明:用来改变文件所有者的识别号(owner id)或者它的用户组识别号(group ID),如若两个参数owner或group中的任意一个为-1,则对应的ID不变。
原型:int chown (char *pathname, uid_t owner,gid_t group);
int fchown (int fd, uid_t owner,gid_t group);
返回值:调用成功返回0,失败返回-1
示例代码:
- #include<sys/types.h>
- #include<sys/stat.h>
- #include<unistd.h>
- #include<fcntl.h>
- #include<stdio.h>
- #include<stdlib.h>
- #include<errno.h>
- #include<string.h>
- #include<dirent.h>
- #define ERR_EXIT(m) \
- do { \
- perror(m); \
- exit(EXIT_FAILURE); \
- } while(0)
- int main(int argc, char *argv[])
- {
- DIR *dir = opendir(".");
- struct dirent *de;
- while ((de = readdir(dir)) != NULL)
- {
- if (strncmp(de->d_name, ".", 1) == 0)
- continue; //忽略隐藏文件
- printf("%s\n", de->d_name);
- }
- closedir(dir);
- exit(EXIT_SUCCESS); // 等价于return 0
- }
四、文件长度
stat结构成员st_size表示以字节为单位的文件长度。此字段只对普通文件、目录文件盒符号链接有意义。
对于普通文件,其文件长度可以是0,在读这种文件时,将得到文件结束指示。
对于目录,文件长度通常是一个数(例如512)的倍数。
对于符号链接,文件长度是文件名中的实际字节数(即huangcheng的符号链接为10,不包括c语言用作名字结尾的null字符)
五、文件截短
- int truncate(const char *path, off_t length);
- int ftruncate(int fd, off_t length);
这两个函数将把现有的文件长度截短length字节。如果该文件以前的长度大于length,则超过length以外的数据就被删除。
六、chdir、fchdir和getcwd函数
每个进程都有一个当前目录,此目录是搜索所有相对路径名的起点。当前工作目录是进程的一个属性,起始目录则是登陆名(/etc/passwd)的一个属性。
- int chdir(const char *path);
- int fchdir(int fd);
这两个函数中,分别用path或打开文件描述符指定新的当前工作目录。
- char *getcwd(char *buf, size_t size);
把当前目录的绝对地址保存到 buf 中,buf 的大小为 size。如果 size太小无法保存该地址,返回 NULL 并设置 errno 为 ERANGE。可以采取令 buf 为 NULL并使 size 为负值来使 getcwd 调用 malloc 动态给 buf 分配,但是这种情况要特别注意使用后释放缓冲以防止内存泄漏。
UNIX环境高级编程——文件和目录的更多相关文章
- UNIX 环境高级编程 文件和目录
函数stat , fstat , fstatat , lstat stat函数返回与此文件有关的信息结构. fstat函数使用已打开的文件描述符(而stat则使用文件名) fstatat函数 为一个相 ...
- UNIX环境高级编程——文件I/O
一.文件描述符 对于Linux而言,所有对设备或文件的操作都是通过文件描述符进行的.当打开或者创建一个文件的时候,内核向进程返回一个文件描述符(非负整数).后续对文件的操作只需通过该文件描述符,内核记 ...
- UNIX环境高级编程 文件I/O
大多数文件I/O 只需要用到 5个函数 : open , read , write , lseek , close 本章描述的都是不带缓冲的I/O(read write 都调用内核中的一个系统调 ...
- UNIX环境高级编程---标准I/O库
前言:我想大家学习C语言接触过的第一个函数应该是printf,但是我们真正理解它了吗?最近看Linux以及网络编程这块,我觉得I/O这块很难理解.以前从来没认识到Unix I/O和C标准库I/O函数压 ...
- (三) 一起学 Unix 环境高级编程 (APUE) 之 文件和目录
. . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...
- [置顶] 文件和目录(二)--unix环境高级编程读书笔记
在linux中,文件的相关信息都记录在stat这个结构体中,文件长度是记录在stat的st_size成员中.对于普通文件,其长度可以为0,目录的长度一般为1024的倍数,这与linux文件系统中blo ...
- (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO
. . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...
- (四) 一起学 Unix 环境高级编程(APUE) 之 系统数据文件和信息
. . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...
- apue.h头文件(UNIX环境高级编程)
在看UNIX环境高级编程是,碰到一个头文件"apue.h",搜一下别人的帖子,其实apue.h是作者自己写的一个文件,包含了常用的头文件,系统不自带.其中包含了常用的头文件,以及出 ...
随机推荐
- 将jdbc连接明文密码加密方案
最近没有及时写文章,把最近处理的几个问题集中了一下写出来.这篇文章是关于如何处理spring项目中引入数据库连接等 使用的用户名和密码的明文进行加密.防止被他人窃取利用. 我们选择的加密方式为DES加 ...
- DotnetSpider (二) Downloader的设置 Request自定义数据字典
本篇主要分享自定义Downloader和Request信息,实现自定义请求内容,及将自定义内容存储. ** 温馨提示:如需转载本文,请注明内容出处.** 本文连接:http://www.cnb ...
- NOIP2017D2T3 列队—Treap
NOIP2017列队 Description Sylvia 是一个热爱学习的女孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Sylvia所在的方阵中有n × m ...
- 安卓高级1 -----Xutil3 和Picasso使用
Xutils3 Xutils由于内部使用httpclient然而在安卓5.0谷歌发现httpclient出现不稳定的情况.于6.0完全弃用,所以作者升级到Xutils3替换原本网络模块 配置环境(St ...
- GCT学习总结
GCT的一个综合的考试性质,时间紧,题量大,这个时候需要我们快速.准确的答题,把自己的能力展现在其中,十一期间和同学们一起学习.讨论,大家都提高很大,各科谈一下自己的心得 数学: 数学相对来说还是不难 ...
- list标准函数的模拟
;反序 ( ) -> ( ) (define (rvs x) (let recur ((x x)(res '())) (if (null? x) res (recur (cdr x) (cons ...
- RxJava操作符(07-辅助操作)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51658445 本文出自:[openXu的博客] 目录: Delay Do Materiali ...
- Erlang简单并行服务器
Erlang简单并行服务器(金庆的专栏)Erlang并行服务器为每个Tcp连接创建对应的连接进程,处理客户端数据.参考 Erlang程序设计(第2版)17.1.3 顺序和并行服务器并行服务器的诀窍是: ...
- XML之DOM解析模型
<?xml version= "1.0" encoding = "UTF-8"> <articles> <article cate ...
- 在Activity,Service,Window中监听Home键和返回键的一些思考,如何把事件传递出来的做法!
在Activity,Service,Window中监听Home键和返回键的一些思考,如何把事件传递出来的做法! 其实像按键的监听,我相信很多人都很熟练了,我肯定也不会说这些基础的东西,所以,前期,还是 ...