[英]C language: error: ‘O_DIRECTORY’ undeclared and accessing files in directory and sub-directory(without using opendir())
我想遍历所有子目录并获取目录和子目录中的所有文件。 我只想使用open()和read()系统调用来执行此操作(而不要使用opendir()或is_dir),我一直在遇到错误
error: ‘O_DIRECTORY’ undeclared (first use in this function)
尽管我确实导入了fcntl。
这是我的代码如下:
#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;
}
另外,如何使用read()系统调用来检查是否正在读取文件或子目录? (正如我提到的,我也想遍历所有子目录中的文件)我知道is_dir可以做到这一点,但是我正在寻找一种无需使用它的方式。 任何帮助,将不胜感激。
引用圣经 :
O_CLOEXEC
,O_DIRECTORY
和O_NOFOLLOW
标志在POSIX.1-2001中未指定,但在POSIX.1-2008中指定。 由于glibc的2.12,人们可以通过定义或者获取它们的定义_POSIX_C_SOURCE
具有大于或等于一个值200809L
或_XOPEN_SOURCE
具有大于或等于一个值700
。 在glibc 2.11和更早版本中,可以通过定义_GNU_SOURCE
获得定义。
尝试使用-D_POSIX_C_SOURCE=200809L
编译,或在包含任何标头之前添加#define _POSIX_C_SOURCE 200809L
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.