Linux libc-doc
Files | |
file | dirent.h |
file | stdio.h |
file | stat.h |
file | types.h |
Functions | |
DIR * | opendir (const char *name) |
Open a directory. More... | |
int | closedir (DIR *dirp) |
Close a directory. More... | |
struct dirent * | readdir (DIR *dirp) |
Read an entry of a directory. More... | |
FILE * | fopen (const char *pathname, const char *mode) |
Open a file. More... | |
int | fclose (FILE *fp) |
Close a file. More... | |
int | stat (const char *path, struct stat *buf) |
Retrieve metadata of a file. More... | |
int | lstat (const char *path, struct stat *buf) |
Retrieve metadata of a file. More... | |
Detailed Description
Checking whether a regular file exists, open it, and close it:
Iterating over all entries of a directory. Be aware, that readdir() also returns hidden files (starting with a .
) including the two entries pointing to the current directory (.
) and the parent directory (..
).
Function Documentation
DIR* opendir | ( | const char * | name | ) |
The opendir() function opens a directory stream according to name
. The stream is positioned at the first entry of the directory. Opened directories must be closed by closedir().
- Parameters
-
name name of the directory to be opened
- Return values
-
DIR* on success NULL on error, errno
is set
int closedir | ( | DIR * | dirp | ) |
A directory opened by the opendir() function, can be closed by the closedir() function, which frees all allocated resources.
We do not expect error handling when closing directories, so simply do:
- Parameters
-
dirp directory stream to be closed
struct dirent* readdir | ( | DIR * | dirp | ) |
The readdir() function reads the next entry from an opened directory stream pointed to by dirp
. It allocates a struct dirent
structure and returns a pointer to the allocated structure containing the information about the next directory entry. The caller of readdir() must not provide (or free) memory for the struct dirent
structure.
readdir() returns NULL
if an error occurs or if the end of the directory stream is reached. To be able to distinguish these two events, a caller must set the errno
variable to 0
before each call of readdir(). If errno
is still 0
after readdir() returned NULL
the end of the directory stream has been reached, otherwise an error has occurred.
The struct dirent
contains information about a directory entry. The most important information are the inode number and the name of the entry:
- Parameters
-
dirp directory stream to read next entry from
- Return values
-
dirent* pointer to the next directory entry NULL on error ( errno
is set) or if the end of the directory stream is reached
FILE* fopen | ( | const char * | pathname, |
const char * | mode | ||
) |
The fopen() (file open) function opens the file at pathname
with the mode as specified in mode
. Opened files must be closed by fclose().
The path in pathname
can specify a relative (based on the current working directory) or an absolute path.
Valid file modes are:
Mode | Description |
---|---|
r | read only |
r+ | read and write |
w | write only, create file if it does not exist yet |
w+ | read and write file, create file if it does not exist yet |
a | write only, append only, create file if it does not exist yet |
a+ | write append only, read from beginning only, create file if it does not exist yet |
- Parameters
-
pathname path to file mode file mode
- Return values
-
FILE* on success NULL on error, errno
is set
int fclose | ( | FILE * | fp | ) |
int stat | ( | const char * | path, |
struct stat * | buf | ||
) |
The stat() function retrieves information about the file pointed to by path
. If path
is a symbolic link, stat() returns information about the underlying file instead of the link itself. Be aware that the caller is responsible to provide the memory for the struct stat
structure pointed to by buf
!
The struct stat
contains, amongst others, the following information:
The st_mode
field encodes the file type and permissions. In order to check whether a file is regular file, a symbolic link, or a directory, some macros exist:
- Parameters
-
path file to be analyzed buf pointer to a buffer storing the retrieved information
- Return values
-
0 on success -1 on error, errno
is set
int lstat | ( | const char * | path, |
struct stat * | buf | ||
) |
The lstat() function retrieves information about the file pointed to by path
. If path
is a symbolic link, lstat() returns information about the link itself instead of the underlying file. Be aware that the caller is responsible to provide the memory for the struct stat
structure pointed to by buf
!
For more details see the stat() function.
- Parameters
-
path file to be analyzed buf pointer to a buffer storing the retrieved information
- Return values
-
0 on success -1 on error, errno
is set