Reputation: 21
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
int main (void)
{
DIR *dp;
struct dirent *ep;
dp = opendir ("./");
if (dp != NULL)
{
while (ep = readdir (dp))
puts (ep->d_name);
(void) closedir (dp);
}
else
perror ("Couldn't open the directory");
return 0;
}
This code gives me all things in that directory. It works like "ls" command. For example let's say i have one folder which is name is "folder" and one .txt file which is name is "input" (names are could be different btw) , in that directory. I want to decide if it is folder or txt file.If it is txt file how can i read it?
Upvotes: 0
Views: 494
Reputation: 119
All information about a file except the contents can be get by calling function stat
. stat
has signature
int stat(const char *pathname, struct stat *buf);
and returns information about the file in the buffer pointed to by buf
.
struct stat
encodes information of file type and file permission in the field st_mode
.
Some additional macros are defined to test the file type:
S_ISREG(m) is it a regular file?
S_ISDIR(m) directory?
S_ISCHR(m) character device?
S_ISBLK(m) block device?
S_ISFIFO(m) FIFO (named pipe)?
S_ISLNK(m) symbolic link? (Not in POSIX.1-1996.)
S_ISSOCK(m) socket? (Not in POSIX.1-1996.)
Here is a piece of sample code:
stat(pathname, &sb);
if (S_ISREG(sb.st_mode)) {
/* Handle regular file */
}
As for reading, concatenate directory path and filename to get file path, using function sprintf
, like this:
sprintf(file_path, "%s/%s", dir_path, file_name);
Upvotes: 1
Reputation: 3364
]You can use scandir()
function to open and scan the entries in a directory.
The example comes from man 3 scandir
#define _SVID_SOURCE
/* print files in current directory in reverse order */
#include <dirent.h>
int
main(void)
{
struct dirent **namelist;
int n;
n = scandir(".", &namelist, NULL, alphasort);
if (n < 0)
perror("scandir");
else {
while (n--) {
printf("%s\n", namelist[n]->d_name);
free(namelist[n]);
}
free(namelist);
}
}
Note the struct dirent
:
struct dirent {
ino_t d_ino; /* inode number */
off_t d_off; /* offset to the next dirent */
unsigned short d_reclen; /* length of this record */
unsigned char d_type; /* type of file; not supported
by all file system types */
char d_name[256]; /* filename */
};
you can check if current entry is a regular file, directory or others by d_type
field.
Available file types are :
#define DT_UNKNOWN 0
#define DT_FIFO 1
#define DT_CHR 2
#define DT_DIR 4 // directory type
#define DT_BLK 6
#define DT_REG 8 // regular file, txt file is a regular file
#define DT_LNK 10
#define DT_SOCK 12
#define DT_WHT 14
After checking the name and type of file, you can safely open it with open()
(system call) or fopen()
(glib function) and read the contents by read()
(if you opened file via open() or fread()
(fopen() counterpart).
DON'T forget to close file after read.
Besides, if you just want to check the existence and accessibility of a directory, access()
is handle.
The code below tests if the dir exist.
int exist_dir (const char *dir)
{
DIR *dirptr;
if (access(dir, F_OK) != -1) {
// file exists
if ((dirptr = opendir(dir)) != NULL) {
// do something here
closedir (dirptr);
} else {
// dir exists, but not a directory
return -1;
}
} else {
// dir does not exist
return -1;
}
return 0;
}
Upvotes: 1