open: Used to Open the file for reading, writing or both.
Syntax in C language
int open (const char* Path, int flags [, int mode ]);
Parameters
Path:
path to file which you want to use use absolute path begin with “/”, when you are not work in same directory of file.
Use relative path which is only file name with extension, when you are work in same directory of file.
flag:
O_RDONLY: read only,
O_WRONLY: write only,
O_RDWR: read and write,
O_CREAT: create file if it doesn’t exist,
O_EXCL: prevent creation if it already exists
How it works in OS
Find the existing file on disk
Create file table entry
Set first unused file descriptor to point to file table entry
Return file descriptor used, -1 upon failure
#include<stdio.h>
#include<fcntl.h>
#include<errno.h>
extern int errno;
void main()
{
int fd = open("foo.txt", O_RDONLY | O_CREAT);
printf("fd = %d/n", fd);
}
Output:
fd = 3
|