Linux O_WRONLY 未声明(首次在此函数中使用)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16726377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:01:12  来源:igfitidea点击:

O_WRONLY undeclared (first use in this function)

clinuxubuntu

提问by Nejthe

#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?

我该如何解决?

回答by runner

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

我已经在我的机器(Ubuntu 12.0.4)上尝试过这段代码。但是我没有收到像您一样的任何错误消息。

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

根据您的手册页,open()您可能丢失了#include <sys/stat.h>

回答by Stefan van den Akker

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

@凯文是对的。在我的 Arch 安装中,根据man fcntl.h,您需要#include <fcntl.h>访问O_WRONLY.

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

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

回答by Antti Haapala

Manual pages for open(2):

手册页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.

请确认您拥有所有必需的包含内容。