1.相关shell指令
在linux的urbantu系统之中进行指令的使用
date:显示当前的本地时间
date -u:显示当前的世界标准时间
time:显示程序运行的时间
cal:查看日历
2.程序获取日历时间
函数功能:获取日历时间
函数头文件 #include<time.h>
函数的原型 time_t time(time_t *tloc);
函数返回值 成功返回 时间秒数 失败返回 返回 -1
日历时间转换成本地时间
函数的功能: 将获取到的日历秒数转化为本地时间
函数的头文件:#include<time.h>
函数的原型: struct tm*localtime(const time_t *timep);
函数的参数: const time_t *timep:日历时间
函数的返回值 成功返回 本地时间的结构体指针 失败返回 NULL
时间结构体
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) */
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 */
};
printf("mytime=%ld\n", mytime);
获取格林尼治时间
函数的功能: 将获取到的日历秒数转化为格林尼治时间
函数的头文件:#include<time.h>
函数的原型: struct tm *gmtime(const time_t *timep);
函数的参数: const time_t *timep:日历时间
函数的返回值 成功返回 格林尼治时间结构体 失败返回 NULL
获取本地的是时间
函数的功能: 将获取到的日历秒数转化为本地时间
函数的头文件:#include<time.h>
函数的原型: char *ctime(const time_t *timep);
函数的参数: const time_t *timep:日历时间
函数的返回值 成功返回 本地时间的字符串 失败返回 NULL
时间函数不可以连续使用两次
3.文件属性的获取
1.stat
函数的功能 获取文件的属性
函数的头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
函数的原型
int stat(const char *pathname, struct stat *statbuf);
函数的参数
const char *pathname, 路径
struct stat *statbuf 文件属性的结构体
函数的返回值 成功返回 0 失败返回 -1
+++++++++++++++++++++++++++++++++++++++++++++++++++
struct stat {
dev_t st_dev; 设备文件表述符
ino_t st_ino; 节点号
mode_t st_mode; 文件类型和权限
nlink_t st_nlink; 硬链接数
uid_t st_uid; 所有者的ID
gid_t st_gid; 组id’
dev_t st_rdev; 设备id
off_t st_size; 总大小
blksize_t st_blksize;
blkcnt_t st_blocks;
struct timespec st_atim; 最后访问时间
struct timespec st_mtim; 最后的修改时间
struct timespec st_ctim; 最后一次状态改变的时间
#define st_atime st_atim.tv_sec /* Backward compatibility */
#define st_mtime st_mtim.tv_sec
#define st_ctime st_ctim.tv_sec
};
2.fstat
函数的功能 获取文件的属性
函数的头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
函数的原型 int fstat(int fd, struct stat *statbuf);
函数的参数
int fd, 文件描述符
struct stat *statbuf 文件属性结构体
函数的返回值 成功返回 0 失败返回 -1
3.lstat
函数的功能 获取文件的属性
函数的头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
函数的原型 int lstat(const char *pathname, struct stat *statbuf);
函数的参数
const char *pathname, 文件路径
struct stat *statbuf 文件属性的结构体
函数的返回值 成功返回 0 失败返回 -1
4.三种文件属性获取到区别
- stat和lstat都不用打开文件即可获取文件的属性,fstat 需要打开文件然后才能获取文件的属性
- stat和lstat在打开普通文件的时候是一样的,当对象为链接文件的时候
- (Stat获取到的是链接文件指向的文件的属性,Lstat获取到的是链接文件的属性)这两者均是软连接,硬连接则无此之分
4.目录操作
1:创建目录 mkdir
函数的功能: 创建一个空目录
函数的头文件
#include <sys/stat.h>
#include <sys/types.h>
函数的原型
int mkdir(const char *pathname, mode_t mode);
函数的参数
const char *pathname, 要创建的目录的名字
mode_t mode 权限
函数的返回值 成功返回 0 失败返回 -1
2:删除目录 rmdir
函数的功能 删除一个空目录
函数的头文件 #include <unistd.h>
函数的原型
int rmdir(const char *pathname);
函数的参数 const char *pathname:要删除的目录文件的名字
函数的返回值 成功返回 0 失败返回 -1
3:获取当前目录 pwd
函数的功能: 获取当前目录
函数的头文件 #include <unistd.h>
函数的原型
char *getcwd(char *buf, size_t size);
函数的参数
char *buf, 目录的名字
size_t size 设定的路径长度
函数的返回值 成功返回 当前的路径 失败返回 NULL
4:更改文件权限 chmod
函数的功能: 修改文件的权限
函数的头文件 #include <sys/stat.h>
函数的原型 int chmod(const char *pathname, mode_t mode);
函数的参数
const char *pathname, 要修改权限的文件
mode_t mode 权限
函数的返回值 成功返回 0 失败返回 -1
5:切换目录 cd
函数的功能: 切换目录
函数的头文件 #include <unistd.h>
函数的原型 int chdir(const char *path);
函数的参数 const char *path:要切换的目录
函数的返回值 成功返回 0 失败返回 -1
6:打开目录(重点)
函数的功能: 打开一个目录
函数的头文件
#include <sys/types.h>
#include <dirent.h>
函数的原型
DIR *opendir(const char *name);
函数的参数
const char *name:要打开的目录
函数的返回值 成功返回 目录流指针 失败返回 NULL
7:关闭目录(重点)
函数的功能: 关闭一个目录
函数的头文件
#include <sys/types.h>
#include <dirent.h>
函数的原型 int closedir(DIR *dirp);
函数的参数 DIR *dirp :目录流指针
函数的返回值 成功返回 0 失败返回 -1
8:读取目录(重点)
函数的功能: 读取目录流里的内容
函数的头文件 #include <dirent.h>
函数的原型 struct dirent *readdir(DIR *dirp);
函数的参数 DIR *dirp:目录流指针
函数的返回值 成功返回 文件信息的结构体指针 失败返回 NULL
++++++++++++++++++++++++++++++++++++++
struct dirent {
ino_t d_ino; 节点号
off_t d_off; 偏移量
unsigned short d_reclen; 长度 记录的实际长度,
并非sizeof(struct dirent)的值,
而是sizeof(struct dirent) - sizeof(name) + {strlen(name) 补齐8字节对齐}。
unsigned char d_type; / 文件类型 不用关心
char d_name[256]; 文件名//用->
};
9:获得目录读写指针偏移量(重点)
函数的功能 获取读写指针距离目录流的头的偏移量
函数的头文件 #include <dirent.h>
函数的原型 long telldir(DIR *dirp);
函数的参数 DIR *dirp:目录流
函数的返回值 成功返回 当前目录流指针距离头的偏移量 失败返回 -1
10:重新定位目录流读写指针(重点)
函数的功能: 将目录流指针偏移到指定的位置
函数的头文件 #include <dirent.h>
函数的原型 void seekdir(DIR *dirp, long loc);
函数的参数 DIR *dirp, 目录流指针 long loc 偏移量
函数的返回值 无
11:重新将目录流指针定位到开头位置
函数的功能: 将目录流指针偏移到开头的位置
函数的头文件
#include <sys/types.h>
#include <dirent.h>
函数的原型 void rewinddir(DIR *dirp);
函数的参数 DIR *dirp:目录流指针
函数的返回值 无