G
G
Grigory Dikiy2015-11-04 20:29:56
linux
Grigory Dikiy, 2015-11-04 20:29:56

Library for reading directories?

Good day, I'm looking for a library to work with the Linux file system. I am writing an analogue of a dialog box with a choice of a directory for saving (such a task from the university). To draw the interface I use SDL2.
What is needed from the library:

  1. Reading a directory
  2. Distinguish files from folders (for icon and transition to it)

Watched dirent.h suits everything, except that it does not distinguish files from folders. There is an idea to check if the folder is trying to open in this way: directory name + file / folder in the directory + "/" and if it opens (true), then mark it as a folder, but I suspect this approach is wrong, if only because the folder may have different rights or something like.
Are there any analogues of this library, can I somehow define the folder through diren.h ?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
AtomKrieg, 2015-11-04
@AtomKrieg

distinguishes dirent folders from files

ent = readdir(curr_dir);
switch (ent->d_type)
{
// directory
case DT_DIR:	break;
//files
case DT_REG:	break;
}

A
Alexey S., 2015-11-04
@Winsik

unsigned char d_type; /* type of file; not supported
by all file system types */
www.gnu.org/software/libc/manual/html_node/Directo...

<skip>
DT_DIR
    A directory.
<skip>

A
abcd0x00, 2015-11-05
@abcd0x00

The stat() system call yields a structure in which all properties are specified.

The  following  symbolic names for the values of type mode_t shall also
be defined.

File type:

S_IFMT Type of file.

S_IFBLK
       Block special.

S_IFCHR
       Character special.

S_IFIFO
       FIFO special.

S_IFREG
       Regular.

S_IFDIR
       Directory.
...

The following macros shall be provided to test whether a file is of the
specified  type.  The  value  m  supplied to the macros is the value of
st_mode from a stat structure.  The macro shall evaluate to a  non-zero
value if the test is true; 0 if the test is false.

S_ISBLK(m)
       Test for a block special file.

S_ISCHR(m)
       Test for a character special file.

S_ISDIR(m)
       Test for a directory.

S_ISFIFO(m)
       Test for a pipe or FIFO special file.

S_ISREG(m)
       Test for a regular file.

S_ISLNK(m)
       Test for a symbolic link.

S_ISSOCK(m)
       Test for a socket.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question