O_WRONLY未声明(首次使用此功能)

Nej*_*the 10 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" );
} }
Run Code Online (Sandbox Code Playgroud)

这是我的代码,我收到了错误

O_WRONLY undeclared (first use in this function)
O_CREAT undeclared (first use in this function)
O_EXCL undeclared (first use in this function)
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

Ste*_*ker 15

@Kevin是对的.根据您的Arch安装,man fcntl.h您需要#include <fcntl.h>访问O_WRONLY.

要使用open(),您还需要#include <sys/stat.h>.


run*_*ner 6

我在我的机器上尝试过这段代码(Ubuntu 12.0.4).但是我没有收到任何错误信息.

根据open()你可能遗失的手册页#include <sys/stat.h>.