C open() Function: Guide to Usage, Flags & Error Handling

目次

1. What is the C language open function?

1.1 Role of the open function

The C language open function is a system call used to open or create files. It is commonly used in Linux and UNIX-like OS environments and is employed for lower-level file operations compared to the standard library function fopen.

1.2 Differences from the fopen function

The C language also provides the standard library function fopen for opening files, but its purpose differs from the open function.
FunctionFeatures
openLow-level API, returns a file descriptor, system call
fopenHigh-level API, returns a FILE*, supports buffering
Uses of the open function
  • Logging to a file (using O_APPEND)
  • Creating temporary files (O_TMPFILE)
  • Asynchronous operations (O_NONBLOCK)
  • Creating files with specific permissions (O_CREAT + mode)
Thus, the open function is used for a wide range of purposes, from simple file operations to advanced file management.

2. open Function Basic Usage (Sample Code Included)

2.1 open Function Prototype

open function is defined in the fcntl.h header file and is declared as follows.
#include <fcntl.h>

int open(const char *path, int flags, mode_t mode);
  • path: Path of the file to open (e.g., "sample.txt").
  • flags: Flags that specify the file open mode and behavior (e.g., O_RDONLY).
  • mode: Permissions used when a file is created (required when O_CREAT is specified).
  • Return value:
  • On success: a file descriptor (an integer >= 0)
  • On failure: returns -1 and stores the error details in errno.

2.2 Basic Usage of the open Function

The following example opens a file in read/write mode (O_RDWR) and, if creating a new file, uses permissions 0644.
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main() {
    int fd = open("sample.txt", O_RDWR | O_CREAT, 0644);
    if (fd1) {
        perror("open");
        return 1;
    }

    printf("File opened successfully with descriptor %dn", fd);

    close(fd);
    return 0;
}

2.3 Error Handling for the open Function

When the open function fails, it returns -1 and stores the error code in the global variable errno. Using perror() displays the error message in a readable way.
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>

int main() {
    int fd = open("/root/protected.txt", O_RDONLY);
    if (fd == -1) {
        perror("open failed");
        return 1;
    }

    close(fd);
    return 0;
}
Example error codes:
  • EACCES (Permission denied): insufficient privileges
  • ENOENT (No such file or directory): the specified file does not exist

2.4 Using Options with the open Function

  • Open for read-only
  int fd = open("sample.txt", O_RDONLY);
  • Open for write-only
  int fd = open("sample.txt", O_WRONLY);
  • Create if the file does not exist
  int fd = open("sample.txt", O_WRONLY | O_CREAT, 0644);
  • Open an existing file and truncate its contents
  int fd = open("sample.txt", O_WRONLY | O_TRUNC);
年収訴求

3. open Function Flags List [Complete Guide]

3.1 Basic Flags

3.1.1 Flags to specify read/write mode

FlagDescription
O_RDONLYOpen for read-only
O_WRONLYOpen for write-only
O_RDWROpen in read/write mode

3.1.2 Example Usage

int fd1 = open("file.txt", O_RDONLY);  // read-only
int fd2 = open("file.txt", O_WRONLY);  // write-only
int fd3 = open("file.txt", O_RDWR);    // read/write

3.2 Flags related to file creation and management

3.2.1 O_CREAT (create new file if it does not exist)

int fd = open("newfile.txt", O_WRONLY | O_CREAT, 0644);

3.2.2 O_EXCL (used with O_CREAT, error if file already exists)

int fd = open("newfile.txt", O_WRONLY | O_CREAT | O_EXCL, 0644);
if (fd == -1) {
    perror("File already exists");
}

3.2.3 O_TRUNC (truncate file contents when opened)

int fd = open("log.txt", O_WRONLY | O_TRUNC);

3.3 Flags that control write behavior

3.3.1 O_APPEND (open in append mode)

int fd = open("log.txt", O_WRONLY | O_APPEND);
write(fd, "New log entryn", 14);

3.4 Flags that control asynchronous/special behavior

3.4.1 O_NONBLOCK (non-blocking mode)

int fd = open("fifo_pipe", O_RDONLY | O_NONBLOCK);

3.4.2 O_SYNC (synchronous writes)

int fd = open("important_data.txt", O_WRONLY | O_SYNC);

3.4.3 O_NOFOLLOW (do not follow symbolic links)

int fd = open("symlink_file", O_RDONLY | O_NOFOLLOW);

3.5 Combining Flags

PurposeFlag Combination
Read/write + create newO_RDWR | O_CREAT, 0644
Write-only + append modeO_WRONLY | O_APPEND
Read-only + non-blockingO_RDONLY | O_NONBLOCK
Open file and truncate contentsO_WRONLY | O_TRUNC
Create new only if file does not existO_WRONLY | O_CREAT | O_EXCL, 0644

4. Details of mode (file permissions)

4.1 What is mode?

mode is the value used to specify file access permissions when creating a new file with the open function (using the O_CREAT flag).
int fd = open("newfile.txt", O_WRONLY | O_CREAT, 0644);

4.2 Basic mode Settings

Permission ValueAccess RightsMeaning
0 (none)---No access
1 (execute)--xExecute only
2 (write)-w-Write only
4 (read)r--Read only
6 (read + write)rw-Read & write
7 (all permissions)rwxRead, write, execute

4.3 Relationship between mode and umask

On UNIX-like operating systems, the specified mode value is not applied directly; it is affected by the umask (user mask).
0666 (specified mode)
- 0022 (umask)
------------
0644 (actual permission applied)
Check the current umask:
$ umask
0022

4.4 Changing permissions with chmod

If you want to change permissions after creation, use the chmod command.
$ chmod 600 secret.txt  # secret file
$ chmod 755 script.sh   # executable file
When using chmod in C:
#include <sys/stat.h>

chmod("file.txt", 0644);

4.5 Practical example of open function considering mode

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main() {
    int fd = open("secure.txt", O_WRONLY | O_CREAT, 0600);
    if (fd == -1) {
        perror("open failed");
        return 1;
    }
    printf("File created successfully with descriptor %dn", fd);
    close(fd);
    return 0;
}

5. open function and related system calls

5.1 close function (close a file)

5.1.1 close prototype

#include <unistd.h>

int close(int fd);

5.1.2 close usage example

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main() {
    int fd = open("sample.txt", O_RDONLY);
    if (fd == -1) {
        perror("open failed");
        return 1;
    }

    printf("File opened successfully.n");

    if (close(fd) == -1) {
        perror("close failed");
        return 1;
    }

    printf("File closed successfully.n");

    return 0;
}

5.2 read function (read data from a file)

5.2.1 read prototype

#include <unistd.h>

ssize_t read(int fd, void *buf, size_t count);

5.2.2 read usage example

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main() {
    int fd = open("sample.txt", O_RDONLY);
    if (fd == -1) {
        perror("open failed");
        return 1;
    }

    char buffer[128];
    ssize_t bytesRead = read(fd, buffer, sizeof(buffer) - 1);

    if (bytesRead == -1) {
        perror("read failed");
        close(fd);
        return 1;
    }

    buffer[bytesRead] = '�';
    printf("Read data: %sn", buffer);

    close(fd);
    return 0;
}

5.3 write function (write data to a file)

5.3.1 write prototype

#include <unistd.h>

ssize_t write(int fd, const void *buf, size_t count);

5.3.2 write usage example

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main() {
    int fd = open("output.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (fd == -1) {
        perror("open failed");
        return 1;
    }

    const char *data = "Hello, World!n";
    ssize_t bytesWritten = write(fd, data, 14);

    if (bytesWritten == -1) {
        perror("write failed");
        close(fd);
        return 1;
    }

    printf("Written %ld bytes to file.n", bytesWritten);

    close(fd);
    return 0;
}

5.4 lseek function (change position within a file)

5.4.1 lseek prototype

#include <unistd.h>

off_t lseek(int fd, off_t offset, int whence);

5.4.2 lseek usage example

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main() {
    int fd = open("sample.txt", O_RDONLY);
    if (fd == -1) {
        perror("open failed");
        return 1;
    }

    off_t newPos = lseek(fd, 5, SEEK_SET);
    if (newPos == -1) {
        perror("lseek failed");
        close(fd);
        return 1;
    }

    char buffer[10];
    read(fd, buffer, 5);
    buffer[5] = '�';
    printf("Read after seek: %sn", buffer);

    close(fd);
    return 0;
}

6. open Practical Usage Examples

6.1 Open a log file in append mode

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main() {
    int fd = open("log.txt", O_WRONLY | O_CREAT | O_APPEND, 0644);
    if (fd == -1) {
        perror("open failed");
        return 1;
    }

    const char *log_entry = "New log entryn";
    write(fd, log_entry, 14);

    close(fd);
    return 0;
}

6.2 Create a temporary file

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main() {
    int fd = open("/tmp", O_TMPFILE | O_RDWR, 0644);
    if (fd == -1) {
        perror("open failed");
        return 1;
    }

    const char *data = "Temporary datan";
    write(fd, data, 15);

    printf("Temporary file created (fd = %d)n", fd);

    close(fd);
    return 0;
}

6.3 Asynchronous processing using O_NONBLOCK

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main() {
    int fd = open("fifo_pipe", O_RDONLY | O_NONBLOCK);
    if (fd == -1) {
        perror("open failed");
        return 1;
    }

    printf("File opened in non-blocking moden");

    close(fd);
    return 0;
}

6.4 Implementing error handling

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>

int main() {
    int fd = open("/root/protected.txt", O_RDONLY);
    if (fd == -1) {
        perror("open failed");

        if (errno == EACCES) {
            printf("Permission deniedn");
        } else if (errno == ENOENT) {
            printf("File does not existn");
        }

        return 1;
    }

    close(fd);
    return 0;
}

6.5 Changing file position using lseek

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main() {
    int fd = open("sample.txt", O_RDONLY);
    if (fd == -1) {
        perror("open failed");
        return 1;
    }

    lseek(fd, 10, SEEK_SET);

    char buffer[11];
    read(fd, buffer, 10);
    buffer[10] = '�';

    printf("Read after seek: %sn", buffer);

    close(fd);
    return 0;
}

7. open Frequently Asked Questions (FAQ) about the open function

7.1 What is the difference between the open function and the fopen function?

FunctionFeaturesUse Cases
openSystem call that returns a file descriptor (integer)Low-level file operations, system programming
fopenStandard C library that returns a FILE*General file I/O that utilizes buffering

7.2 Is it always necessary to specify mode with the open function?

  • Only required when using O_CREAT
int fd = open("newfile.txt", O_WRONLY | O_CREAT, 0644);

7.3 Does specifying O_APPEND always make the write position at the end?

  • Yes, specifying O_APPEND causes writes to always occur at the end
int fd = open("log.txt", O_WRONLY | O_APPEND);
write(fd, "New entryn", 10);
close(fd);

7.4 When would you use the O_NONBLOCK flag?

  • When you want immediate return on FIFOs (pipes) or sockets
int fd = open("fifo_pipe", O_RDONLY | O_NONBLOCK);
if (fd == -1) {
    perror("open failed");
}

7.5 What happens when you open a symbolic link with the open function?

  • By default it opens the target file, but you can prevent this with O_NOFOLLOW
int fd = open("symlink.txt", O_RDONLY | O_NOFOLLOW);
if (fd == -1) {
    perror("open failed");
}

7.6 How to get a file size with the open function?

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main() {
    int fd = open("sample.txt", O_RDONLY);
    if (fd == -1) {
        perror("open failed");
        return 1;
    }

    off_t filesize = lseek(fd, 0, SEEK_END);
    printf("File size: %ld bytesn", filesize);

    close(fd);
    return 0;
}

7.7 What are the main reasons the open function fails?

Error CodeMeaning
EACCESPermission denied
ENOENTFile does not exist
EEXISTExisting file when using O_CREAT | O_EXCL
EMFILEExceeded the process’s limit of open files
ENOSPCInsufficient disk space
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>

int main() {
    int fd = open("/root/protected.txt", O_RDONLY);
    if (fd == -1) {
        perror("open failed");

        if (errno == EACCES) {
            printf("Permission deniedn");
        } else if (errno == ENOENT) {
            printf("File does not existn");
        }

        return 1;
    }

    close(fd);
    return 0;
}

8. Summary

8.1 Basics of the open function

  • open function is a system call for opening or creating files.
  • Unlike fopen, it returns a file descriptor (FD).
  • Properly call close(fd) to prevent resource leaks.

8.2 Main flags of the open function

FlagDescription
O_RDONLYOpen for read‑only access
O_WRONLYOpen for write‑only access
O_RDWROpen for read/write access
O_CREATCreate a new file if it does not exist
O_TRUNCTruncate the existing file’s contents
O_APPENDAlways append data to the end when writing
O_NONBLOCKNon‑blocking mode
O_NOFOLLOWDo not follow symbolic links

8.3 System calls related to the open function

  • close(fd): releases the file descriptor.
  • read(fd, buf, count): reads data from a file.
  • write(fd, buf, count): writes data to a file.
  • lseek(fd, offset, whence): moves the file read/write pointer.

8.4 Practical examples of using the open function

  • Appending to log files → use O_APPEND to add at the end.
  • Creating temporary files → leverage O_TMPFILE.
  • Asynchronous processing → avoid blocking with O_NONBLOCK.

8.5 Caveats for the open function

  • Properly call close(fd): to prevent resource leaks.
  • Perform error handling: check errno and take appropriate action.
  • Set permissions correctly: a mistake in specifying mode can become a security risk.

8.6 Summary of this article

SectionContent
open function basicsSystem call for low‑level file operations
open function usageWhen using O_CREAT, a mode is required
Main flags of openO_RDONLY, O_WRONLY, O_RDWR, O_CREAT, etc.
Related system callsclose, read, write, lseek
Practical examplesLog recording, temporary files, asynchronous processing, error handling
FAQDifferences from fopen, behavior of O_APPEND, error handling

8.7 Conclusion

The C language open function is an essential system call for system‑level file operations. Using it correctly enables efficient file management and asynchronous processing. Apply the knowledge learned in this article to develop safe and robust programs.