Linux中的 open,close,write,read函数

在Linux系统中一切的操作都是基于文件系统实现的。

文件系统中最常见的几个函数就是博文所介绍的函数。

1.open函数:

头文件:#include <fcntl.h>

函数原型:int open(const char *pathname, int oflag, ... /* mode_t mode */);

pathname:所要打开的文件路径的字符串如"/user/desktop/linux/code/file1"

oflag:一些打开的宏定义,表示打开方式。是只读、只写、还是创建...

/* mode_t mode */:(可以不写)表示文件创建的权限

return value:文件句柄fd

以下是oflag的宏定义: 

  O_RDONLY 只读模式

  O_WRONLY 只写模式

  O_RDWR 读写模式

  打开/创建文件时,至少得使用上述三个常量中的一个。

        以下常量是选用的:

  O_APPEND 每次写操作都写入文件的末尾

  O_CREAT 如果指定文件不存在,则创建这个文件

  O_EXCL 如果要创建的文件已存在,则返回 -1,并且修改 errno 的值

  O_TRUNC 如果文件存在,并且以只写/读写方式打开,则清空文件全部内容(即将其长度截短为0)

  O_NOCTTY 如果路径名指向终端设备,不要把这个设备用作控制终端。

  O_NONBLOCK 如果路径名指向 FIFO/块文件/字符文件,则把文件的打开和后继 I/O

demo1:

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

int main()
{
    int fd;

    fd = open("./file1", O_RDWR);

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

运行结果是屏幕打印一个文件句柄,结果是

fd = 3

demo2:

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

int main()
{
    int fd;

    fd = open("./file1", O_RDWR);
    if(fd == -1)
    {
        printf("fail to open\n");
        fd = open("./file1", O_RDWR | O_CREAT, 0600);
        if(fd > 0)
        {
            printf("creat/open successfully.\n");
            printf("fd = %d\n", fd);
        }
    }

}

这个demo演示了open函数第三个参数mode的有无,和第二个参数的或逻辑。运行函数时,如果当前目录下没有file1文件,会先打印fail to open,再创建一个权限为0600(当前用户可读可写)的file1,并且打印该文件的文件描述符(fd)。

demo3:

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

int main()
{
    int fd;
    char *buf = "this is a message from linux c.";

    fd = open("./file1", O_RDWR);
    if(fd == -1)
    {
        printf("fail to open\n");
    }
    printf("fd = %d\n", fd);
    fd = open("./file1", O_RDWR | O_CREAT, 0600);
    if(fd > 0)
    {
        printf("creat/open successfully.\n");
    }
    printf("fd = %d\n", fd);

    write(fd, buf, strlen(buf));

    close(fd);

    return 0;
}

这个demo是写一句话到file1里面。

用到了write函数

头文件:#include <unistd.h>

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

fd:写到哪个文件(句柄)

*buf:写什么(字符串)

count:写多长

return value:就是count

后面使用到的read函数也是一样的

 

头文件:#include <unistd.h>

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

fd:从哪个文件读

*buf:读到哪去

count:读多长

return value:count

 close函数比较简单。就是将文件句柄作为实参传进去,就可以实现关闭文件的操作。

int close(int fd);

返回值可以用来是否关闭成功。

但是要注意的是,每次对于文件的操作一定要记得close或者正常关闭不然可以会出现一些不好的影响。比如我们使用vim编辑器编辑文件的时,如果使用CTRL + Z强制关闭,那么文件列表里面就会生成swp文件,再次打开原文件的时候,系统就会出错。

demo4:(头文件没有写完整)

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int fd;
    char *buf = "this is a message from linux c.";

    fd = open("./file1", O_RDWR);
    if(fd == -1)
    {
        printf("fail to open\n");
        fd = open("./file1", O_RDWR | O_CREAT, 0600);
        if(fd > 0)
        {
            printf("open succeddfully\n");
        }
    }
    printf("fd = %d\n", fd);

    int n_write = write(fd, buf, strlen(buf));
    if( n_write != -1 )
    {
        printf("write %d byte to file\n", n_write);
    }

    char *readbuf;
    readbuf = (char *)malloc(sizeof(char) * n_write + 1);

    int n_read = read(fd, readbuf, n_write);
    printf("n_read = %d\n", n_read);
    printf("form file1: %s\n", readbuf);

    close(fd);

    return 0;
}

这段demo运行结果和预期不同。想要从file1 中读取数据到readbuf中却读取失败。是因为光标的问题。可以使用关闭文件重新打开或者使用 lseek函数。

使用close()解决后的函数(头文件不完整):


int main()
{
    int fd;
    char *buf = "this is a message from linux c.";

    fd = open("./file1", O_RDWR);
    if(fd == -1)
    {
        printf("fail to open\n");
        fd = open("./file1", O_RDWR | O_CREAT, 0600);
        if(fd > 0)
        {
            printf("open succeddfully\n");
        }
    }
    printf("fd = %d\n", fd);

    int n_write = write(fd, buf, strlen(buf));
    if( n_write != -1 )
    {
        printf("write %d byte to file\n", n_write);
    }
    close(fd);
    fd = open("./file1", O_RDWR);

    char *readbuf;
    readbuf = (char *)malloc(sizeof(char) * n_write + 1);

    int n_read = read(fd, readbuf, n_write);
    printf("n_read = %d\n", n_read);
    printf("form file1: %s\n", readbuf);

    close(fd);

    return 0;
}

运行结果:

fd = 3
write 31 byte to file
n_read = 31
form file1: this is a message from linux c.

以上就是Linux C中的 open,close,write,read函数介绍和demo演示。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值