Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

O_WRONLY undeclared (first use in this function)

Tags:

c

linux

ubuntu

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
char data [ 6 ];
main ( ) {
int len;
desc = open ( "Resultat", O_WRONLY | O_CREAT | O_EXCL, 0666 );
if ( desc != -1 ) {
len = write ( desc, &data, sizeof ( data ) );
if ( len != sizeof ( data ) )
printf ( "ERROR" );
} }

this is my code and i'm getting the error

O_WRONLY undeclared (first use in this function)
O_CREAT undeclared (first use in this function)
O_EXCL undeclared (first use in this function)

How do I fix that?

like image 628
Nejthe Avatar asked May 24 '13 01:05

Nejthe


3 Answers

@Kevin is right. On my Arch installation, according to man fcntl.h, you need to #include <fcntl.h> to get access to O_WRONLY.

To use open(), you also need to #include <sys/stat.h>.

like image 141
Stefan van den Akker Avatar answered Nov 04 '22 14:11

Stefan van den Akker


I have tried this code in my machine (Ubuntu 12.0.4). But I didn't get any error messages like you got.

According to the man page of open() you are probably missing #include <sys/stat.h>.

like image 6
runner Avatar answered Nov 04 '22 15:11

runner


Manual pages for open(2):

#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);

Please verify that you have each and every one of the required includes.