[英]C language: error: ‘O_DIRECTORY’ undeclared and accessing files in directory and sub-directory(without using opendir())
I want to loop through all the sub-directories and get all the files in the directory and sub-directories. 我想遍历所有子目录并获取目录和子目录中的所有文件。 I only want to use the open() and read() system call to do so(not opendir() or is_dir) bit I keep on getting an error
我只想使用open()和read()系统调用来执行此操作(而不要使用opendir()或is_dir),我一直在遇到错误
error: ‘O_DIRECTORY’ undeclared (first use in this function)
although I did imported the fcntl. 尽管我确实导入了fcntl。
here is my code down below: 这是我的代码如下:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include<fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
extern int errno;
int main()
{
int fd;
if ((fd = open(".", O_DIRECTORY | O_RDONLY)) ==-1)
{
printf("error %s\n", strerror(errno));
return -1;
}
return 0;
}
In addition how can use the read() system call to check if I'm reading a file or a sub-directory? 另外,如何使用read()系统调用来检查是否正在读取文件或子目录? (as I mentioned I want to go also over the files in all the sub-directories) I know is_dir does that but I'm looking for a way to do it without using it.
(正如我提到的,我也想遍历所有子目录中的文件)我知道is_dir可以做到这一点,但是我正在寻找一种无需使用它的方式。 Any help would be appreciated.
任何帮助,将不胜感激。
Quoting the holy scripture : 引用圣经 :
The
O_CLOEXEC
,O_DIRECTORY
, andO_NOFOLLOW
flags are not specified in POSIX.1-2001, but are specified in POSIX.1-2008.O_CLOEXEC
,O_DIRECTORY
和O_NOFOLLOW
标志在POSIX.1-2001中未指定,但在POSIX.1-2008中指定。 Since glibc 2.12, one can obtain their definitions by defining either_POSIX_C_SOURCE
with a value greater than or equal to200809L
or_XOPEN_SOURCE
with a value greater than or equal to700
.由于glibc的2.12,人们可以通过定义或者获取它们的定义
_POSIX_C_SOURCE
具有大于或等于一个值200809L
或_XOPEN_SOURCE
具有大于或等于一个值700
。 In glibc 2.11 and earlier, one obtains the definitions by defining_GNU_SOURCE
.在glibc 2.11和更早版本中,可以通过定义
_GNU_SOURCE
获得定义。
Try compiling with -D_POSIX_C_SOURCE=200809L
or add a #define _POSIX_C_SOURCE 200809L
before including any headers. 尝试使用
-D_POSIX_C_SOURCE=200809L
编译,或在包含任何标头之前添加#define _POSIX_C_SOURCE 200809L
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.