[英]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.