目录
1 访问目录 – opendir
2 访问目录 – readdir
3 访问目录 – closedir
4 修改文件访问权限 – chmod/fchmod
5 获取文件属性 – stat/lstat/fstat
6 文件属性 – struct stat
7 文件类型 – st_mode
8 文件访问权限 – st_mode
9 练习 – 获取并显示文件属性
掌握:读取目录、修改文件访问权限、获取文件属性的知识
opendir函数用来打开一个目录文件:
#include
DIR *opendir(const char *name);
DIR *fdopendir(int fd); 使用文件描述符,要配合open函数使用
readdir函数用来读取目录流中的内容:
#include
struct dirent *readdir(DIR *dirp);
struct dirent {
ino_t d_ino; /* inode number */
off_t d_off; /* not an offset; see NOTES */
unsigned short d_reclen; /* length of this record */
unsigned char d_type; /* type of file; not supported by all filesystem types */
char d_name[256]; /* dir name */
};
closedir函数用来关闭一个目录文件:
#include
int closedir(DIR *dirp);
成功时返回0;出错时返回EOF
示例:
int main(int argc, char *argv[]) {
{
DIR *dirp;
struct dirent *dp;
if (argc < 2) {
printf(“Usage : %s \n”, argv[0]); return -1;
}
if ((dirp = opendir(argv[1])) == NULL) {
perror(“opendir”); return -1;
}
while ((dp = readdir(dirp)) != NULL) {
printf(“%s\n”, dp->d_name);
}
closedir(dirp);
综合示例
#include
#include
int main(int argc,char **argv){
DIR* dp;
struct dirent *dt;
dp=opendir("/mnt/hgfs/share/newIOP/");
if(dp == NULL){
perror("opendir");
return 0;
}
while((dt=readdir(dp))!=NULL){
printf("%s\n",dt->d_name);
}
closedir(dp);
}
chmod/fchmod函数用来修改文件的访问权限:
#include
int chmod(const char *path, mode_t mode);
int fchmod(int fd, mode_t mode);
示例:
chmod(“test.txt”, 0666);
示例:
#include
#include
int main(int argc,char **argv){
int ret;
ret = chmod("temp",0444);
if(ret<0){
perror("chmod");
return 0;
}
}
stat/lstat/fstat函数用来获取文件属性:
#include
int stat(const char *path, struct stat *buf); //buf中表示文件属性
int lstat(const char *path, struct stat *buf);
int fstat(int fd, struct stat *buf);
struct stat是存放文件属性的结构体类型:
struct stat {
dev_t st_dev; //文件的设备编号
ino_t st_ino; //节点
mode_t st_mode; //文件的类型和存取的权限
nlink_t st_nlink; //连到该文件的硬连接数目,刚建立的文件值为1
uid_t st_uid; //用户ID
gid_t st_gid; //组ID
dev_t st_rdev; //(设备类型)若此文件为设备文件,则为其设备编号
off_t st_size; //文件字节数(文件大小)
unsigned long st_blksize; //块大小(文件系统的I/O 缓冲区大小)
unsigned long st_blocks; //块数
time_t st_atime; //最后一次访问时间
time_t st_mtime; //最后一次修改时间
time_t st_ctime; //最后一次改变时间(指属性)
};
通过系统提供的宏来判断文件类型:
S_IFMT 0170000 文件类型的位遮罩
S_ISREG(st_mode) 0100000 是否常规文件
S_ISDIR(st_mode) 0040000 是否目录
S_ISCHR(st_mode) 0020000 是否字符设备
S_ISBLK(st_mode) 0060000 是否块设备
S_ISFIFO(st_mode) 0010000 是否FIFO文件
S_ISLNK(st_mode) 0120000 是否链接文件
S_ISSOCK(st_mode) 0140000 是否SOCKET文件
通过系统提供的宏来获取文件访问权限:
S_IRUSR 00400 bit:8 所有者有读权限
S_IWUSR 00200 7 所有者拥有写权限
S_IXUSR 00100 6 所有者拥有执行权限S_IRGRP 00040 5 群组拥有读权限
S_IWGRP 00020 4 群组拥有写权限
S_IXGRP 00010 3 群组拥有执行权限
S_IROTH 00004 2 其他用户拥有读权限
S_IWOTH 00002 1 其他用户拥有写权限
S_IXOTH 00001 0 其他用户拥有执行权限
以下面格式打印指定文件的主要信息:
$ ./a.out test.c -rw-r--r-- 317 2014-11-08 test.c
调用lstat函数获取文件的属性
从stat结构体中获取相应信息并输出
#include
#include
#include
#include
#include
int main (int argc,char **argv){
struct stat buf;
int ret;
if(argc < 2)
{
printf("Usage: %s \n",argv[0]);
return -1;
}
ret = stat(argv[0],&buf);
if(ret<0){
perror("stat");
return 0;
}
if(S_ISREG(buf.st_mode)){
printf("-");
}
if(S_ISDIR(buf.st_mode)){
printf("d");
}
if(S_ISCHR(buf.st_mode)){
printf("c");
}
if(S_ISBLK(buf.st_mode)){
printf("b");
}
if(S_ISFIFO(buf.st_mode)){
printf("p");
}
if(S_ISSOCK(buf.st_mode)){
printf("s");
}
// printf(" ");
int i;
for(i=8;i>=0;i--){
if(buf.st_mode & (1<tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min);
printf("%s\n",argv[0]);
}
遍历一个文件夹下所有文件,并打印文件大小和日期
#include
#include
#include
#include
#include
#include
int main(int argc,char * argv[])
{
int ret;
DIR *dp;
struct stat buf;
struct dirent *dt;
struct tm *t;
if(argc < 2)
{
printf("Usage: %s \n",argv[0]);
return -1;
}
dp = opendir(argv[1]);
if(dp == NULL)
{
perror("opendir");
return -1;
}
while((dt=readdir(dp)) != NULL)
{
printf("%10s ",dt->d_name);
if( stat(dt->d_name,&buf) >= 0)
{
printf("%10d ",(int)buf.st_size);
t = localtime(&buf.st_ctime);
printf(" %d-%d-%d %d:%d\n",t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min);
}
else
{
perror("stat");
return -1;
}
}
return 0;
}