Linux系统编程-文件IO

文件IO

open函数

SYNOPSIS

    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>

    int open(const char *pathname, int flags);
    int open(const char *pathname, int flags, mode_t mode);

创建文件最终权限:mode & ~umask

常用参数

O_RDONLY O_WRONLY O_RDWR
O_APPEND O_CREAT O_TRUNC

open 常见错误

  1. 文件不存在
  2. 操作权限不对
  3. 以只写方式打开目录

errno 错误码
使用方法:

    #include <errno.h>
    #include <string.h>
    //错误的操作
    char *strerror(int errnum);

read函数

       #include <unistd.h>

       ssize_t read(int fd, void *buf, size_t count);

size_t count为缓冲区的大小
返回-1时需要进一步判断,详见阻塞和非阻塞

write函数

       #include <unistd.h>

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

size_t为待写入数据的大小

文件描述符

  1. 进程控制块PCB,本质是一个 结构体,其中有成员:文件描述符表,文件描述符本质为指针(键值对映射)
  2. struct file结构体,一个进程最多打开1024个文件,

阻塞和非阻塞

概念:阻塞和非阻塞指的是调用者(程序)在 等待返回结果(或输入)时的状态。 阻塞时,在调用结果返回前,当前线程会被 挂起 ,并在得到结果之后返回。 非阻塞时,如果不能立刻得到结果,则该调用者不会阻塞当前线程。

  • 读取常规文件一般不会阻塞,因为不管其读取多少字节,read一定能在有限时间内得到返回结果。
  • 产生阻塞的场景,读取网络、设备等文件
  • 以O_NONBLOCK打开文件,可以为文件加上非阻塞属性,当一个阻塞文件被以非阻塞方式打开,并且文件无数据,read函数返回-1并且将errno = EAGIN或EWOULDBLOCK,不是read失败。
  • demo:
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <string.h>
    #include <errno.h>
    #include <stdlib.h>

    int main()
    {
        int fd1;
        int len,tim;
        char buf[512];
        //以只读 非阻塞方式打开一个含有阻塞属性的文件
        fd1 = open("/dev/tty",O_RDONLY | O_NONBLOCK);
        if(fd1 < 0)
        {
            perror("open /dev/tty");
            exit(1);
        }
    
    tryagain:

        len = read(fd1, buf, 512);
        if(len < 0)
        {
            if(errno != EAGAIN)
            {
                perror("read /dev/tty");
                exit(1);
            }
            else
            {
                write(STDOUT_FILENO,"try again\n",strlen("try again\n"));
                sleep(1);
                goto tryagain;
            }
            
        }
        write(STDOUT_FILENO,buf,len);
        close(fd1);
        return 0;

    }

fcntl函数

SYNOPSIS
       #include <unistd.h>
       #include <fcntl.h>

       int fcntl(int fd, int cmd, ... /* arg */ );
  • 获取文件状态:F_GETFL
  • 设置文件状态:F_SETFL
  • 返回值:
    • 返回值会有一个位图,每一位代表一个属性,可以通过按位操作来设置或获取文件对应属性

demo:

#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>

int main()
{
    char buf[32];
    int flags = 0;
    int len;
    flags = fcntl(STDIN_FILENO,F_GETFL); //获取文件的属性信息
    if(flags == -1)
    {
        perror("fnctl error");
        exit(1);
    }
    flags = flags | O_NONBLOCK; //按位或操作
    int ret = fcntl(STDIN_FILENO,F_SETFL,flags);
    if(ret == -1)
    {
        perror("fnctlset error");
        exit(1);
    }
tryagain:
    len = read(STDIN_FILENO, buf, 512);
    if(len < 0)
    {
        if(errno != EAGAIN)
        {
            perror("read /dev/tty");
            exit(1);
        }
        else
        {
            write(STDOUT_FILENO,"try again\n",strlen("try again\n"));
            sleep(1);
            goto tryagain;
        }
        
    }
    write(STDOUT_FILENO,buf,len);

    return 0;
}

lseek函数

SYNOPSIS
       #include <sys/types.h>
       #include <unistd.h>

       off_t lseek(int fd, off_t offset, int whence);

参数:

  1. fd 文件描述符
  2. offset 偏移量
  3. whence 起始偏移量
    返回值:
    成功:相较于起始位置偏移量
    失败 -1
  • 文件的读和写使用的同一个偏移量
  • 可以使用lseek函数来获取,拓展文件大小
  • 要使得文件大小被修改,必须使用文件IO操作
  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值