系统函数1

系统函数

man man :查看man文档

man 章节号 函数名 :查找该章节里的函数。

如不加章节号,将会从头开始查找,找到第一个符合条件后停止。

errno

所在目录/usr/include/errno.h

  1. 定义在头文件errno.h中

    1. 全局变量
    2. 任何标准C库函数都能对其修改(Linux系统函数更可以)
  2. 错误宏定义位置

    1. 第1-34个错误定义:

      /usr/include/asm-generic/errno-base.h

    2. 第35-133个错误定义:

      /usr/include/asm-generic/errno.h

  3. 是记录系统的最后一次错误代码。代码是一个int型的值

    1. 每个errno值对应着以字符串表示的错误类型
    2. 当调用“某些”函数出错是,该函数会重新设置errno的值
perror
  1. 头文件:stdio.h
  2. 函数定义:void perror(const char *s)
  3. 函数说明:
    1. 用来将上一个函数发生错误的原因输出到标准设备(stderr)
    2. 参数s所指的字符串会先打印出,后面再加上错误原因字符串
    3. 此错误原因依照全局变量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. -1 读文件失败

  2. 0 文件读完了

  3. >0 读取的字节数
    

write:将buf缓冲区中的count字节写入文件描述符fd所表示的文件中去

size_t write(int fd,const void *buf,size_t count)

头文件同read

返回值:

  1. <count fd有误或磁盘已满
    
  2. 0 没有写入任何数据

  3. -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
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值