Abdul Axundzade
Abdul Axundzade

Reputation: 71

How can I open directory with open() function in C?

I want to open a directory without using <dirent.h>. I tried this:

#include <fcntl.h>

int fd = open("dir", O_RDONLY, 0);

But it returns fd = -1. Why? As I know, the directory is a file too, it just stores the location of children files and directories.

Upvotes: -1

Views: 2173

Answers (1)

Tom V
Tom V

Reputation: 5470

You can open directories with open, but what you probably want is the function opendir.

You will benefit from reading the manual page for open, especially the parts about O_PATH and O_DIRECTORY.

To learn the details of the directory reading ABI, read the source of opendir/readdir in your C library.

Upvotes: 0

Related Questions