I am using x86_64 GNU/Linux with gcc.
SYNOPSIS section of man -s2 open
says:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
int creat(const char *pathname, mode_t mode);
Now when I try to compile the following code snippet, gcc
doesn't throw a warning/error.
#include <stdio.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
int fd;
fd = open("foo.txt", O_RDWR, 0777);
if(fd == -1)
perror("open");
fd = creat("foo.txt", 0777);
if(fd == -1)
perror("creat");
close(fd);
return 0;
}
So are types.h
and stat.h
optional? What purpose do they serve in manpage of open()
?
#include <sys/types. h> #include <sys/stat.
The open and creat functions are declared in the header file fcntl. h , while close is declared in unistd. h .
2. open: Used to Open the file for reading, writing or both. Syntax in C language #include<sys/types.h> #include<sys/stat.h> #include <fcntl.h> int open (const char* Path, int flags [, int mode ]); Parameters. Path: path to file which you want to use.
The open() system call opens the file specified by pathname. If the specified file does not exist, it may optionally (if O_CREAT is specified in flags) be created by open().
The man page serves as an instruction both to programmers and to compiler manufacturers.
It is possible that you don't need to include them on your system. However, the man page describes a portable way to use the methods, so you should include them anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With