(4)Linux基础——open/close、write/read、Iseek详细含义用法及介绍(基础)

一、整体流程

  1. 打开文件:open

  1. 读写文件:read/write

  1. 关闭文件:close

二、open

①函数原型:int open(const char *pathname, int flags);

②功能:打开文件

③参数:

pathname:打开文件的路径

flags:

O_RDONLY :只读

O_WRONLY : 只写

O_RDWR : 读写

O_CREAT :文件不存在创建 (需要加入第三个参数)

O_TRUNC :文件存在截断成0

O_APPEND :追加

O_ASYNC :异步IO

O_NONBLOCK :非阻塞IO

............

④返回值:成功返回很小的非负整数(文件描述符),失败返回-1

eg:

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

int main(int argc, char const *argv[])
{
    int fp =0;

    fp = open("a.txt",O_RDONLY | O_WRONLY | O_CREAT,0664);
    if (-1 == fp)
    {
        perror("fail to open ");
        return -1;
    }

    printf("fp = %d\n",fp);
    return 0;
}

输出结果:

注意:有人肯定会疑惑为什么不是0,1,2,而是3呢?

一直存在三个特殊的文件描述符:stdin 0,stdout 1,stderr 2

三、close

①函数原型:int close(int fd);

②功能:关闭文件

eg: close(fp)

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

int main(int argc, char const *argv[])
{
    int fp =0;

    fp = open("a.txt",O_RDONLY | O_WRONLY | O_CREAT,0664);
    if (-1 == fp)
    {
        perror("fail to open ");
        return -1;
    }

    printf("fp = %d\n",fp);
    close(fp);
}

注意:每次man时要查看该函数的头文件

使用close时就要加上头文件#include <unistd.h>

四、write

①函数原型:ssize_t write(int fd, const void *buf, size_t count);

②功能:向文件描述符中写入buf开始的count个字节

③参数:fd:文件描述符 buf:存放数据空间首地址 count:字节个数

④返回值:成功返回实际写入字节数,失败返回-1, 0表示写入0个字节

eg:

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

int main(int argc, char const *argv[])
{
    int fp =0;
    char ch[4096] = {"hello world"};

    fp = open("a.txt",O_WRONLY | O_CREAT | O_TRUNC,0664);
    if( -1 == fp)
    {
        perror("fail to open ");
        return -1;
    }
    write(fp,ch,sizeof(ch));
    close(fp);
    return 0;
}

输入a.txt结果:

五、read

①函数原型: ssize_t read(int fd, void *buf, size_t count);

②功能:从文件描述符中读取count个字节存放到buf开始的空间中

③参数:fd:文件描述符 buf:存放数据空间首地址 count:字节个数

④返回值:成功返回实际读到字节数,失败返回-1 ,读到文件末尾返回0

eg:

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

int main(int argc, char const *argv[])
{
    int fp =0;
    char ch[4096] = {0};
    size_t  t ;
    fp = open("a.txt",O_RDONLY );
    if( -1 == fp)
    {
        perror("fail to open ");
        return -1;
    }
    
    t =read(fp,ch,sizeof(ch));
    
    printf("实际读到%ld字节\n",t);  
    printf("ch = %s\n",ch);
    close(fp);
    return 0;
}

输出结果:

六、Iseek

①函数原型:off_t lseek(int fd, off_t offset, int whence);

②功能:重新定位文件描述的偏移量

③参数:fd:文件描述符 offset:偏移量

whence:

SEEK_SET文件开头位置

SEEK_CUR:文件当前位置

SEEK_END:文件末尾

④返回值:成功返回当前的偏移量,失败返回-1

eg:

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

int main(int argc, char const *argv[])
{
    int fp = 0;
    off_t len = 0;
    char ch = 0;

    fp = open("a.txt", O_WRONLY | O_TRUNC | O_CREAT, 0664);
    if (-1 == fp)
    {
        perror("fail to open");
        return -1;
    }

    len = lseek(fp, 10, SEEK_SET); //从文件开头向后偏移10个位置,记录文件描述符的偏移量
    printf("len = %ld\n", len);//输出
    ch = 'a';
    write(fp, &ch, 1);//向流中写入字符a

    len = lseek(fp, -5, SEEK_CUR);//从文件当前位置向前偏移5个位置,记录文件描述符的偏移量
    printf("len = %ld\n", len);//输出   (因为之前向后10 + 'a' = 11,因此现在结果为6)
    ch = 'b';
    write(fp, &ch, 1);

    len = lseek(fp, 0, SEEK_SET);//从文件结尾不偏移,记录文件描述符的偏移量
    printf("len = %ld\n", len);// 输出 等于0
    ch = 'c';
    write(fp, &ch, 1);

    close(fp);

    return 0;
}

输出结果:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值