SoFunction
Updated on 2025-03-04

Detailed explanation of the use of C++ open and read functions

For Framework engineers, necessary C or C++ programming capabilities are necessary. For example, the operation of device nodes is the most basic operation, so we will use open and read functions. The open() function is used to open a file, while the read() function is used to read data from the opened file.

1. open() function

The open() function is a POSIX standard function in the C/C++ standard library. It is used to open a file and return a file descriptor (File Descriptor) for subsequent read and write operations. Its function declaration is as follows:

#include <>
int open(const char* path, int flags, mode_t mode);
  • path: The target file name to open or create.
  • flags: modes that perform multiple operations on files, such as O_RDONLY (read-only open), O_WRONLY (write-only open), O_RDWR (read-write-only open), O_CREAT (create a file if the file does not exist), etc.
  • mode: Access permissions for new files, usually using permission masks such as S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH.
  • If the file is opened successfully, open() returns a file descriptor; if it fails, return -1, and set errno to indicate the cause of the error‌12.

2. read() function

The read() function is used to read data from the open file descriptor. Its function declaration is as follows:

#include <>
ssize_t read(int fd, void* buf, size_t count);
  • fd: file descriptor, returned by open() function. buf: A pointer to a buffer used to store read data.
  • count: The number of bytes to be read.
  • If the read is successful, read() returns the actual number of bytes read; if the file ends or an error occurs, return -1, and set errno to indicate the cause of the error‌23.
  • Sample code
  • Here is a simple example showing how to read a file using the open() and read() functions:
#include &lt;&gt;
#include &lt;&gt;
#include &lt;&gt;
#include &lt;&gt;
#include &lt;&gt;
int main() {
    const char* filePath = "";
    int fd = open(filePath, O_RDONLY); // Open the file in read-only mode    if (fd &lt; 0) {
        perror("Failed to open file");
        return -1;
    }
    char buffer[1024]; // Define a buffer to store read data    ssize_t bytesRead = read(fd, buffer, sizeof(buffer)); // Read data from the file to the buffer    if (bytesRead &lt; 0) { // If the reading fails or the file ending character EOF is encountered        perror("Failed to read file");
        close(fd); // Close the file descriptor        return -1;
    }
    printf("Read %ld bytes from file: %s\n", bytesRead, buffer); // Print the read content and number of bytes    close(fd); // Close the file descriptor    return 0;
}

This is the article about the use of C++ open() and read() functions. For more related contents of C++ open() and read() functions, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!