目录操作
1.打开目录opendir
2.读目录readdir
3.关闭目录closedir
mkdir
void error(int status, int errnum, const char *format, ...);
功能:打印自定义的错误信息
error(1, errno, "%s : %s : %d :open error: aaa", __FILE__, __func__, __LINE__);
参数:
status:状态值
0:SUCCESS
1:FAIL
errnum:错误码:errno
format:格式化后错误信息字符串
c语言内置宏:
__FILE__表示是那个文件
__LINE__表示第几行
__func__表示在那个函数// __FUNCTION__
__DATE__
DIR *opendir(const char *name);
功能:打开一个目录并获得一个目录流指针
参数:
name:目录名
返回值:
成功:目录流指针
失败:NULL
struct dirent *readdir(DIR *dirp);
功能:读取目录中的文件信息
参数:
dirp:目录流指针
返回值:
成功:返回文件信息的结构体指针
失败:NULL
struct dirent {
ino_t d_ino; /* Inode number */
off_t d_off; /* Not an offset; see below */
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]; /* Null-terminated filename */
};
int closedir(DIR *dirp);
功能:关闭一个目录流
int mkdir(const char *pathname, mode_t mode);
功能:创建一个目录
参数:
pathname:目录名
mode:对目录的读写执行权限0777
返回值:
成功:0
失败:-1
char *getcwd(char *buf, size_t size);
功能:获取当前工作路径
参数:
buf: 存储当前路径的空间
size:空间大小
返回值:
成功:buf的首地址
失败:NULL
int chdir(const char *path);
功能:修改当前工作路径
参数:
path:新的工作路径
返回值:
成功:0
失败:-1
chmod八进制值 文件名---》修改该文件的读写执行权限
chmod 0777 1.txt
pwd获取当前目录对应的绝对路径
时间相关函数
time
ctime
localtime
time_t time(time_t *tloc);
功能:获取1970-1-1 0:0:0到现在的秒数
参数:
tloc:保存秒数的变量地址
返回值:
返回秒数
char *ctime(const time_t *timep);
功能:将秒数转换成字符串时间
参数:
timep:秒数的地址
返回值:
返回时间字符串
struct tm *localtime(const time_t *timep);
功能:将秒数转换成日历时间
参数:
timep:秒数的地址
返回值:
返回具体时间的结构体指针
struct tm {
int tm_sec; /* Seconds (0-60) */
int tm_min; /* Minutes (0-59) */
int tm_hour; /* Hours (0-23) */
int tm_mday; /* Day of the month (1-31) */11.其他
int tm_mon; /* Month (0-11) */
int tm_year; /* Year - 1900 */
int tm_wday; /* Day of the week (0-6, Sunday = 0) */
int tm_yday; /* Day in the year (0-365, 1 Jan = 0) */
int tm_isdst; /* Daylight saving time */
};