linux下的标准文件库
查询文件属性
头文件
<sys/types.h> <sys/stat.h>
函数
-
int stat(const char *filename, struct stat *buf)
获取文件属性
-
int fstat(int fd, struct stat *buf)
功能同stat,但是入参是文件描述符
-
int lstat(const char *filename, struct stat *buf)
功能同 stat,但是能处理链接文件
stat结构体
> dev_t st_dev //设备ID
mode_t st_mode //文件类型与权限
nlink_t st_nlink //文件链接数 uid_t st_uid //文件所有人 ID gid_t st_gid //文件所属组 ID off_t st_size //文件大小 time_t st_atime //最近访问时间 time_t st_mtime //最近修改时间 time_t st_ctime //文件状态最近改变时间
文件类型
- S_IFREG 普通文件 -
- S_IFDIR 目录文件 d
- S_IFCHR 字符文件 c
- S_IFBLK 块文件 b
- S_IFIFO 管道文件 p
- S_IFLNK 符号链接 l
- S_IFSOCK 套接字文件
- S_IFMT 掩码
判断方法: if (st_mode & S_IFMT)
宏判断, 返回 0或者1 S_ISREG(st_mode) 判断是否为普通文件 -
S_ISDIR(st_mode) 判断是否为目录文件 d
S_ISCHR(st_mode) 判断是否为字符文件 c
S_ISBLK(st_mode) 判断是否为块文件 b
S_ISFIFO(st_mode) 判断是否为管道文件 p
S_ISLNK(st_mode) 判断是否为符号链接 l
S_ISSOCK(st_mode) 判断是否位套接字文件
文件访问权限属性
S_IRUSR S_IWUSR S_IXUSR
S_IRGRP S_IWGRP S_IXGRP
S_IROTH S_IWOTH S_IXOTH
判断方法:if (st_mode & S_IRUSR)
查询文件系统相关信息
头文件
#include <sys/statfs.h> 或者下面这个 #include<sys/vfs.h>
函数
int statfs(const char *path, struct statfs *buf)
int fstatfs(int fd, struct statfs *buf)
statfs结构体
long f_type; /* 文件系统类型 */
long f_bsize; /* 经过优化的传输块大小 */
long f_blocks; /* 文件系统数据块总数 */ long f_bfree; /* 可用块数 */ long f_bavail; /* 非超级用户可获取的块数 */ long f_files; /* 文件结点总数 */ long f_ffree; /* 可用文件结点数 */ fsid_t f_fsid; /* 文件系统标识 */ long f_namelen; /* 文件名的最大长度 */
文件操作
-
FILE* fopen(const char* filename, const char* mode)
打开文件
-
FILE* freopen(const char* filename, const char* mode, FILE* stream)
文件重定向 例如:
FILE *pFile pFile= freopen(“1.txt”, “w”, stderr) // 所有输出到stderr的内容被输出到 1.txt pFile = freopen(“1.txt”, “r”, stdin) //所有有stdin输入的地方,变成从 1.txt 输入
-
int fclose(FILE* stream)
关闭文件
-
int remove(const char *filename)
删除文件
-
int rename(const char *oldname, const char * newname)
重命名文件
标准输入
-
int getc(FILE *stream)
从文件流中读取字符,宏定义方式,效率高,括号里面不要进行运算
-
int fgetc(FILE *stream)
从文件中读取字符,函数定义形式,效率比getc()低
-
int getchar(void)
从stdin 读入一个字符, 事实上,就是 getc(stdin)
-
char *gets(char *s)
从stdin 中读取字符存入 s,返回NULL或者s地址
-
char *fgets(char *s, int n, FILE *stream)
加入流长度控制
格式化输入
- int scanf(const char *format, ...)
- int fscanf(FILE stream, const char format, ...)
- int sscanf(const char* s, const char* format, ...)
标准输出
-
putc(int c, FILE *stream)
-
putchar(int c)
-
fput(int c, FILE *stream)
行输出:以 ‘\0’ 结束输出
-
int puts(const char *s)
-
int fputs(const char *s, FILE *stream)
格式化输出
- int printf()
- int fprintf()
- int sprintf()
块读写
- size_t fread(void *ptr, size_t size, size_t nBlock, FILE *stream)
- size_t fwrite(const void *ptr, size_t size, size_t nBlock, FILE *stream)
文件状态
-
ferror(FILE*)
发生IO错误 返回非0,正常返回0
-
feof(FILE*)
文件结束EOF返回非0, 否则返回0
-
clearer(FILE*)
清除IO错误标志和 EOF标志
文件缓冲
BUFSIZ 缓冲区大小的宏,256的整数倍
-
void setbuf(FILE*, char *buf)
把文件缓冲区设置位 buf, 这里buf大小位 BUFSIZ
buf为NULL则关闭缓存
-
int setvbuf(FILE*, char *buf, int type, size_t size)
指定缓冲区位置(buf)和大小(size)
type 指定缓冲模式
-
_IOFBF:全缓冲:
普通文件读写,缓冲区满时写入文件,读出文件直到缓冲区满
-
_IOLBF:行缓冲:
遇到换行(‘\n’)刷新缓冲区, stdout, stdin
-
_IONBF:无缓冲:
所有IO直接执行, stderr
-
-
int fflush(FILE *);
强制刷新缓冲区
变长参数使用
头文件: #include<stdarg.h>
va_list: 定义变量列表变量
va_start: 获取变量列表起始地址
va_arg: 获取变量,同时指向下一个变量
va_end: 结束变量获取,释放句柄(清零变量)
vsprintf/vsnprintf:从变量列表里依次格式化取变量
变长参数常用来写日记文件 例如:
#include<stdio.h>
#include<stdarg.h> #include<string.h> #include<unistd.h> #include<fcntl.h> #include<time.h> int WriteLog(char *pBuf,...) { va_list vl; va_start(vl,pBuf); char logBuf[256]={}; time_t tmtime; time(&tmtime); struct tm *pTime; pTime=localtime(&tmtime); strftime(logBuf,255,"%Y-%m-%d %X",pTime); int ret=vsnprintf(logBuf+strlen(logBuf),strlen(logBuf)-1,pBuf,vl); if(!ret) { printf("%d\n",ret); perror("vsnprintf failed!"); return -2; } va_end(vl); FILE *pFile; pFile=fopen("log.txt","a"); if(pFile==NULL) { perror("open file failed!"); return -1; } fprintf(pFile,"%s\n\r",logBuf); fclose(pFile); return 0; }