Reputation: 139
I want to use open_excl(3) in order to open a file for exclusive writing.
I wrote:
#include <open.h>
int main(int c, char* v[]){
int fp = open_excl("my_file");
return 0;
}
Now: gcc -Wall file.c -o out.a
And I get a fatal compiler error: open.h: No such file or directory
How come? do I have a broken path problem? Missing a link to a library? Wrong version of gcc? I'm using 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)
Upvotes: 0
Views: 3249
Reputation: 134008
The open_excl
is not a standard function; I don't have open.h
on my Linux system. As the documentation on linux.die.net says:
open_excl
opens the filefilename
for writing and returns the file handle. The file may not exist before the call toopen_excl
. The file will be created with mode0600
.[...]
open_excl
relies on theO_EXCL
flag to open [...]
Thus you could achieve the same with
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags, mode_t mode);
By calling it as follows:
int fd = open(filename, O_EXCL|O_CREAT|O_WRONLY, 0600);
To wrap the file descriptor into FILE *
use the fdopen
function:
#include <stdio.h>
FILE *fp = fdopen(fd);
Upvotes: 3
Reputation: 363
Whenever you include any header file, the compiler looks for the same in standard directory if angular brackets are used #include <open.h>
and in project directory if quotes are used #include "open.h"
.
So you can first check if you have the open.h file in the standard directory (which probably is not the case), you can download and copy the header file in your local directory and use quotes include the same.
Upvotes: -1