目录
1.DIR *opendir(const char *name);
fileno fp转fd
fdopen fd转fp
opendir和readdir
1.DIR *opendir(const char *name);
功能:
打开一个目录获得一个目录流指针
参数:
name:目录名
返回值:
成功返回目录流指针
失败返回NULL
2.readdir
struct dirent *readdir(DIR *dirp);
功能:
从目录流中读取文件信息并将保存信息的结构体
地址返回
参数:
dirp:目录流指针
返回值:
包含文件信息的结构体
出错或者读到目录流末尾返回NULL
3、关闭目录
int closedir(DIR *dirp);
功能:关闭之前已经打开的目录流对象
参数:opendir的返回结果中目录流对象
返回值:成功 0
失败 -1;
getcwd和chdir函数
mkdir 和 rmdir函数
umask==0002 新建文件、新建目录时才起作用
time函数
1.获取秒数
2.转换为需要个格式
系统时间的获取:
1.time
time_t time(time_t *t);
time_t tm;
time(&tm)
tm = time(NULL);
功能:
获得1970年到现在的秒数
参数:
t:存放秒数的空间首地址
返回值:
成功返回1970年到现在的秒数
失败返回-1
ctime函数
localtime函数
练习:完成简单查看文件夹内容功能
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
int main(int argc, char *argv[])
{
DIR * dir = opendir("./");
if(NULL == dir)
{
fprintf(stderr,"opendir errpr\n");
return 1;
}
while(1)
{
struct dirent * info = readdir(dir);
if(NULL == info)
{
break;
}
switch(info->d_type)
{
case DT_DIR:
printf("目录文件 ");
break;
case DT_REG:
printf("普通文件 ");
break;
case DT_UNKNOWN:
printf("其他文件 ");
break;
default:
printf("未知文件 ");
}
printf("%s\n",info->d_name);
}
closedir(dir);
return 0;
}
练习:完成LL查看文件详细详细信息
第一部分权限 10个位,每一位都需要一个判断其权限,其中第一个 ‘-’ , 要在man 7 inode 手册内查询,是一个带参宏,将st.st_mode传入,进行判断 向stdout 屏幕传送数据。
后面九位用手册里面的宏进行与操作判断是或否。
练习:实现ll功能 查看文件夹详细信息
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
struct stat st;
char filename[] = "./1.txt";
int ret = stat(filename,&st);
if(-1==ret)
{
fprintf(stderr, "stat error\n");
return 1;
}
if( S_ISREG(st.st_mode))
{
fputc('-',stdout);
}
else if(S_ISDIR(st.st_mode))
{
fputc('d',stdout);
}
else
{
fputc('o',stdout);
}
//-----------------------------
if(st.st_mode& S_IRUSR)
{
fputc('r',stdout);
}
else
{
fputc('-',stdout);
}
if(st.st_mode& S_IWUSR)
{
fputc('w',stdout);
}
else
{
fputc('-',stdout);
}
if(st.st_mode& S_IXUSR)
{
fputc('x',stdout);
}
else
{
fputc('-',stdout);
}
//----------------------------
if(st.st_mode&S_IRGRP)
{
fputc('r',stdout);
}
else
{
fputc('-',stdout);
}
if(st.st_mode&S_IWGRP)
{
fputc('w',stdout);
}
else
{
fputc('-',stdout);
}
if(st.st_mode& S_IXGRP)
{
fputc('x',stdout);
}
else
{
fputc('-',stdout);
}
//-----------------------------
if(st.st_mode&S_IROTH)
{
fputc('r',stdout);
}
else
{
fputc('-',stdout);
}
if(st.st_mode&S_IWOTH)
{
fputc('w',stdout);
}
else
{
fputc('-',stdout);
}
if(st.st_mode&S_IXOTH)
{
fputc('x',stdout);
}
else
{
fputc('-',stdout);
}
//-rw-rw-r-- 1 linux linux 20 8月 14 10:07 1.txt
printf(" %lu %u %u %lu %s \n",st.st_nlink,st.st_uid,st.st_gid,st.st_mtime,filename);
//printf("ino:%lu mode :%d link:%lu uid:%u gid:%u size:%lu time:%lu %s\n",st.st_ino,st.st_mode,st.st_nlink,st.st_uid
// ,st.st_gid,st.st_size,st.st_mtime,filename);
return 0;
}
练习:找出目标路径中 的 .c 文件
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
// ./a.out ../
int main(int argc, char *argv[])
{
if(argc<2)
{
fprintf(stdout,"usage:./a.out dirname\n");
return 1;
}
DIR * dir = opendir(argv[1]);
if(NULL == dir)
{
fprintf(stderr,"opendir errpr\n");
return 1;
}
while(1)
{
struct dirent * info = readdir(dir);
if(NULL == info)
{
break;
}
// .
if(strlen(info->d_name)>=3)
{
if(0== strcmp(&info->d_name[strlen(info->d_name)-2],".c"))//注意这个字符串数组,给strcmp一个地址,它会从这里往后开始读该数组内存的所有后续字符,限于该行
{
printf("%s\n",info->d_name);
}
}
}
closedir(dir);
return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <fcntl.h>
#include <dirent.h>
int main(int argc, const char *argv[])
{
if(argc<2)
{
fprintf(stderr, "input ./a.out pathname\n");
return 1;
}
DIR *dir = opendir(argv[1]);
if(NULL==dir)
{
fprintf(stderr, "opendir error\n");
return 1;
}
while(1)
{
struct dirent * info = readdir(dir);
if(NULL==info)
{
break;
}
struct stat st;
// char filename[] = info->name;
int ret = stat(info->d_name,&st);
if(-1==ret)
{
fprintf(stderr, "stat error\n");
return 1;
}
if( S_ISREG(st.st_mode))
{
fputc('-',stdout);
}
else if(S_ISDIR(st.st_mode))
{
fputc('d',stdout);
}
else
{
fputc('o',stdout);
}
//-----------------------------
if(st.st_mode& S_IRUSR)
{
fputc('r',stdout);
}
else
{
fputc('-',stdout);
}
if(st.st_mode& S_IWUSR)
{
fputc('w',stdout);
}
else
{
fputc('-',stdout);
}
if(st.st_mode& S_IXUSR)
{
fputc('x',stdout);
}
else
{
fputc('-',stdout);
}
//----------------------------
if(st.st_mode&S_IRGRP)
{
fputc('r',stdout);
}
else
{
fputc('-',stdout);
}
if(st.st_mode&S_IWGRP)
{
fputc('w',stdout);
}
else
{
fputc('-',stdout);
}
if(st.st_mode& S_IXGRP)
{
fputc('x',stdout);
}
else
{
fputc('-',stdout);
}
//-----------------------------
if(st.st_mode&S_IROTH)
{
fputc('r',stdout);
}
else
{
fputc('-',stdout);
}
if(st.st_mode&S_IWOTH)
{
fputc('w',stdout);
}
else
{
fputc('-',stdout);
}
if(st.st_mode&S_IXOTH)
{
fputc('x',stdout);
}
else
{
fputc('-',stdout);
}
time_t tm;
time(&tm);
struct tm *t = localtime(&tm);
//printf("%d-%d-%d %d:%d:%d\n",t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour
// ,t->tm_min,t->tm_sec);
//-rw-rw-r-- 1 linux linux 20 8月 14 10:07 1.txt
printf(" %lu %u %u %lu %d月 %d %d:%d %s \n",st.st_nlink,st.st_uid,st.st_gid,st.st_size,t->tm_mon,t->tm_mday,t->tm_hour,t->tm_min,info->d_name);
}
closedir(dir);
return 0;
}