Use the open, read, and close interfaces in the Linux virtual file system to read a file

1. Create a read function file with the file name testopen.c as follows:

#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. Create the file read.txt that needs to be read and enter the content.

3、

gcc testopen.c -o read

./read

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_33767353/article/details/116035456