open() System Call Example in C

Before a file can be read from or written to, it must be opened. The kernel maintains a per-process list of open files, called the file table.

The most basic method of accessing a file is via the read() and write() system calls.

Before a file can be accessed, however, it must be opened via an open() or creat() call. Once done using the file, it should be closed using the system call close().

The open function is declared in the header file fcntl.h.

The open function is the underlying primitive for the fopen and freopen functions, that create streams.

Here is a simple example:
#include <fcntl.h>
#include <stdio.h>
int main(void){
    int x = open("hello.c",O_RDONLY);
    return 0;
And in action:
koray@koray-VirtualBox:~$ gcc koray.c
koray@koray-VirtualBox:~$ ./a.out