使用Linux虚拟文件系统中的open、read、close接口对某一文件进行读取操作

1、创建读取函数文件,文件名称testopen.c 如下:

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

int main() {
        int fd;
        int rc;
        char buf[512] = {0};
        fd = open("read.txt",O_RDONLY);
        printf("fd:%d\n",fd);
        while(0 < (rc = read(fd,buf,512))){
                printf("%s\n",buf);
        }
        printf("read ok\n");
        close(fd);
        return 0;
}

2、创建需要读取的文件 read.txt ,并输入内容。

3、

gcc testopen.c -o read

./read

猜你喜欢

转载自blog.csdn.net/qq_33767353/article/details/116035456
今日推荐