时间编程
将系统时间转化为字符串时间
ctime 头文件:#include <time.h>
char *ctime(const time_t *timep);功能:将系统时间转化为字符串时间
参数:timep:以秒为单位的时间指针 返回值:成功返回字符串时间 失败返回NULL,错误码
将系统时间转化为自定义时间
struct tm *localtime(const time_t *timep); 头文件 #include <time.h>
功能: 将系统时间转化为自定义时间 参数: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) */ 月+1
int tm_year; /* Year - 1900 */ 年 + 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 */夏令时
};
将自定义时间变成字符串时间
asctime 头文件:#include <time.h>
char *asctime(const struct tm *tm); 功能:将自定义时间转变为字符串时间
参数:tm: struct tm *结构体指针 返回值:成功返回字符串 失败返回NULL,错误码
文件IO
:文件IO的核心就是文件描述符,有系统的地方就会有文件IO
打开文件
open 文件夹:#include <sys/types.h> <sys/stat.h> <fcntl.h>
int open(const char *pathname,int flags);
int open(const char *pathname,int flags,mode_t mode);
功能:文件IO打开文件并返回文件描述符
参数:pathname:带路径的文件名 flags:打开文件的方式
互斥方式-->三选一 O_RDONLY-->只读 O_WRONLY-->只写 O_RDWR-->读写
以下其他方式:O_CREAT:如果文件不存在,则创建 O_EXCL:判断文件在,如果存在,可返回报错-->测试 O_TRUNC:清空模式 O_APPEND:追加模式
mode:创建该文件所使用的权限:0666 0777 O_CREAT
返回值:
成功返回文件描述符
失败返回-1,并设置错误码
操作文件
read 头文件:#include <unistd.h>
ssize_t read(int fd,void *buf,size_t count); 功能:根据文件描述符,读取数据
参数:fd:文件描述符 buf:读取出来的数据存放的位置 count:读取的最大限度
返回值:成功返回读取的字节数 如果读到末尾返回0 失败返回-1,并设置错误码
write 头文件:#include <unistd.h>
ssize_t write(int fd,const void *buf,size_t count);功能:根据文件描述符,写入数据
参数:fd:文件描述符 buf:写入的数据存放位置 count:写入的最大限度
返回值:成功返回写入的字节数 失败返回-1,并设置错误码
关闭文件
close(); 头文件:#include <unistd.h>
int close(int fd); 功能:根据文件描述符,关闭文件 参数:fd:文件描述符
返回值:成功返回0 失败返回-1,并返回错误码
其他函数
lseek();--->文件IO的光标控制 头文件:#include <sys/types.h> <unistd.h>
off_t lseek(int fd,off_t offset,int whence);功能:根据相应的流,控制光标
参数:fd:文件描述符 offset:偏移量 whence:基准位置 SEEK_SET:文件头
SEEK_CUR:当前位置 SEEK_END:文件末尾
返回值:成功返回光标所在的位置 失败返回-1,并设置错误码
文件与目录属性
获取文件信息
利用代码和函数制作一个ls -l 功能
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat(const char *pathname, struct stat *statbuf);
//stat函数获得文件属性的,但获得链接文件时,返回本地文件的信息
int fstat(int fd, struct stat *statbuf);
//fstat函数需要先open文件,才可以获取信息
int lstat(const char *pathname, struct stat *statbuf);
//lstat函数获得文件属性时,如果文件为链接文件,则返回链接文件的信息
struct stat *statbuf;结构体指针
struct stat {
dev_t st_dev; /* ID of device containing file */设备文件
ino_t st_ino; /* Inode number */索引码
mode_t st_mode; /* File type and mode */文件类型与权限
nlink_t st_nlink; /* Number of hard links */硬链接数
uid_t st_uid; /* User ID of owner */拥有者ID
gid_t st_gid; /* Group ID of owner */所属组ID
dev_t st_rdev; /* Device ID (if special file) */设备号
off_t st_size; /* Total size, in bytes */文件大小
blksize_t st_blksize; /* Block size for filesystem I/O */块大小
blkcnt_t st_blocks; /* Number of 512B blocks allocated */块数
/* Since Linux 2.6, the kernel supports nanosecond
precision for the following timestamp fields.
For the details before Linux 2.6, see NOTES. */
struct timespec st_atim; /* Time of last access */最后访问时间
struct timespec st_mtim; /* Time of last modification */最后修改时间
struct timespec st_ctim; /* Time of last status change */文件状态最后修改时间
#define st_atime st_atim.tv_sec /* Backward compatibility */
#define st_mtime st_mtim.tv_sec
#define st_ctime st_ctim.tv_sec
};
根据用户ID获得用户名
getpwuid
#include <sys/types.h>
#include <pwd.h>
struct passwd *getpwuid(uid_t uid);
struct passwd {
char *pw_name; /* username */ 用户名
char *pw_passwd; /* user password */
uid_t pw_uid; /* user ID */
gid_t pw_gid; /* group ID */
char *pw_gecos; /* user information */
char *pw_dir; /* home directory */
char *pw_shell; /* shell program */
};
根据所属组ID获得组名
getgrgid
#include <sys/types.h>
#include <grp.h>
struct group *getgrgid(gid_t gid);
struct group {
char *gr_name; /* group name */ 组名
char *gr_passwd; /* group password */
gid_t gr_gid; /* group ID */
char **gr_mem; /* NULL-terminated array of pointers
to names of group members */
};
库的制作
函数库:本质上来说库是一种可执行代码的二进制形式,可以被操作系统载入内存执行。由于windows和linux的本质不同,因此二者库的二进制是不兼容的。
库的分类:静态库/动态库(共享库)
linux下的库有两种:静态库和动态库 二者的不同点在于代码被载入的时刻不同
静态库在程序编译时会被连接到目标代码中,程序运行时将不再需要该静态库,因此体积比较大。 动态库在程序编译时并不会被连接到目标代码中,而是在程序运行时才被载入,因此在程序运行时还需要动态库存在,因此代码体积较小。
静态库
静态库对函数库的链接是放在编译时期完成的
优点:程序在运行时与函数库无关移植方便
缺点:浪费空间和资源,因为所有相关的对象文件与牵涉到的函数库被链接合成一个可执行文件
动态库
动态库把对一些库函数的链接载入推迟到程序运行的时期
可以实现进程之间的资源共享 将一些程序升级变得简单
甚至可以真正做到链接载入完全由程序员在程序代码中控制