系统函数
man man :查看man文档
man 章节号 函数名 :查找该章节里的函数。
如不加章节号,将会从头开始查找,找到第一个符合条件后停止。
errno
所在目录/usr/include/errno.h
-
定义在头文件errno.h中
- 全局变量
- 任何标准C库函数都能对其修改(Linux系统函数更可以)
-
错误宏定义位置
-
第1-34个错误定义:
/usr/include/asm-generic/errno-base.h
-
第35-133个错误定义:
/usr/include/asm-generic/errno.h
-
-
是记录系统的最后一次错误代码。代码是一个int型的值
- 每个errno值对应着以字符串表示的错误类型
- 当调用“某些”函数出错是,该函数会重新设置errno的值
perror
- 头文件:stdio.h
- 函数定义:void perror(const char *s)
- 函数说明:
- 用来将上一个函数发生错误的原因输出到标准设备(stderr)
- 参数s所指的字符串会先打印出,后面再加上错误原因字符串
- 此错误原因依照全局变量errno的值来决定要输出的字符串
open
光标放在函数上面按shift+k可以直接进入man文档搜索该函数;再按q键可退出
打开已经存在的文件:
格式:fd = open(“文件名”,打开方式);
创建新文件:
格式:fd = open(“新的文件名”,打开方式 | O_CREAT,0777(权限));
关闭文件:
格式:close(fd);
打开方式:
必选项:O_RDINLY,O_WRONLY,O_RDWD
可选项:O_CREAT,O_TRUNC,O_EXCL,O_APPEND
例:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main(){
int fd;
fd = open("hello.c"),O_RDWR);
if(fd==-1)
{
perror("open file");
exit(1);
}
fd = open("myhello",O_RDWR|O_CREAT,0777);
if(fd==-1)
{
perror("open file");
exit(1);
}
printf("fd = %d\n",fd);
int ret = close(fd);
printf("ret = %d\n",ret);
if(ret==-1)
{
perror("open file");
exit(1);
}
}
文件权限:本地有一个掩码(umask查看)
文件的实际权限:给定的权限&本地掩码(取反)=实际的文件权限
例:
777 = 111111111
0022= 111101101
=
111101101 = 755
使用open函数判断文件是否存在
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main(){
int fd = open("myopen",O_RDWD | O_CREAT | O_EXCL,0777);
if(fd==-1)
{
perror("open file");
exit(1);
}
printf("fd = %d\n",fd);
int ret = close(fd);
printf("ret = %d\n",ret);
if(ret==-1)
{
perror("open file");
exit(1);
}
}
将文件截断为0
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main(){
int fd = open("myhello",O_RDWR |O_TRUNC);
if(fd==-1)
{
perror("open file");
exit(1);
}
printf("fd = %d\n",fd);
int ret = close(fd);
printf("ret = %d\n",ret);
if(ret==-1)
{
perror("open file");
exit(1);
}
}
注意:要将open函数的返回值做处理,输出错误
read
size_t read(int fd.const void *buf,size_t count);
头文件
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
返回值:实际读取的字节数
-
-1 读文件失败
-
0 文件读完了
-
>0 读取的字节数
write:将buf缓冲区中的count字节写入文件描述符fd所表示的文件中去
size_t write(int fd,const void *buf,size_t count)
头文件同read
返回值:
-
<count fd有误或磁盘已满
-
0 没有写入任何数据
-
-1 在write调用中出现错误
lseek:对文件描述符fd所表示文件的读写指针进行设置
off_t lseek(int fd,off_t offestmint whence)
功能:1. 移动文件指针
2.获取文件长度
3.文件拓展
whence取值:
SEEK_SET(值为0),则读写指针从文件开头算起移动offest位置。
SEEK_CUR(值为1),则读写指针从当前位置算起移动offest位置。
SEEK_END(值为2),则读写指针从文件结尾算起移动offest位置。
文件拓展案例:
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
int fd = open("aa",O_RDWR);
if(fd==-1)
{
perror("open file");
exit(1);
}
int ret = lseek(fd,0,SEEK_END);
printf("file length = %d\n",ret);
//wenjiantuozhan
ret = lseek(fd,2000,SEEK_END);
printf("return value %d\n",ret);
write(fd,"a",1);
close(fd);
return 0;
}
stat
stat函数
穿透(追踪)函数 --软连接
lstat函数
不穿透(追踪)
access
chmod
作用:改变文件的权限
原型:int chmod(const char *filename,int pmod);
参数:filename:文件名
pmod:权限(必须是一个八进制的数)
返回值:0 成功
-1 失败
chown函数
truncate函数
关于链接
unlink创建临时文件
注意int len