profro
Posts: 45
Joined: Tue Nov 15, 2016 1:26 pm

List files in directory

Tue Apr 11, 2017 7:18 pm

I am trying to list files in a directory and I keep getting "." and ".." in my list. What are these? I have tried to ignore then by doing a strcmp but they keep getting in my list.

Code: Select all

#include <stdio.h>
#include <dirent.h>

int main(void)
{
        char bad_1[1] = ".";
        char bad_2[2] = "..";
        char list_array[20][255];
        int i = 0;
        int j;
        DIR     *d;
        struct dirent *dir;
        d = opendir("/home/pi/Documents/C\040Projects/arrays/Metrology_Data/");

        if (d)
        {
                while ((dir = readdir(d)) != NULL)
                {
                        if (strcmp(dir->d_name,bad_1) == 0)
                        {
                                //do nothing 
                        }
                        if (strcmp(dir->d_name,bad_2) == 0)
                        {
                                //do nothing
                        }
                        else
                        {
                                strcpy(list_array[i], dir->d_name);
                                printf("%s\n", list_array[i]);
                        }       i++;
                }
                closedir(d);
        }
        else
        {
                printf("Open error\n");
        }
        printf("size of array %d\n", i);
        return(0);
}

User avatar
rpdom
Posts: 16114
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: List files in directory

Tue Apr 11, 2017 7:23 pm

The first of those "." is the entry for the directory itself. That's why you often use "./something" in the shell to execute commands in the current directory.

The second ".." is a link to the parent directory. ("cd .." in the shell will take you up one directory level.)

Every directory has these entries except for / which only has . as it has no parent.

They won't normally show, as entries that begin with a "." are classed as hidden. For example, from the command line you would be able to see them with "ls -a"

Code: Select all

pi@raspi9:~ $ ls
rpi_ws281x
pi@raspi9:~ $ ls -a
.  ..  .bash_history  .bash_logout  .bashrc  .profile  rpi_ws281x  .ssh

profro
Posts: 45
Joined: Tue Nov 15, 2016 1:26 pm

Re: List files in directory

Tue Apr 11, 2017 7:46 pm

How would I best ignore them while trying to get a list of the files in a directory?

jahboater
Posts: 5057
Joined: Wed Feb 04, 2015 6:38 pm

Re: List files in directory

Tue Apr 11, 2017 8:27 pm

profro wrote:How would I best ignore them while trying to get a list of the files in a directory?
You probably want "else if" for the second test.
Or something quick and dirty like:

Code: Select all

if( dir->d_name[0] == '.' )
   continue;
will ignore all "hidden" files.

profro
Posts: 45
Joined: Tue Nov 15, 2016 1:26 pm

Re: List files in directory

Wed Apr 12, 2017 12:39 pm

Brilliant! Worked perfectly.

If I may ask, why [0] on the d_name? and why '.' versus "."?

User avatar
Paeryn
Posts: 2808
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: List files in directory

Wed Apr 12, 2017 12:43 pm

profro wrote:Brilliant! Worked perfectly.

If I may ask, why [0] on the d_name? and why '.' versus "."?
Because it is just checking ​if the first character of the string is a fullstop.
She who travels light — forgot something.

profro
Posts: 45
Joined: Tue Nov 15, 2016 1:26 pm

Re: List files in directory

Wed Apr 12, 2017 1:49 pm

So it in effect will filter all names that a "." in the first index of the string? Which catches both "." and ".."

Nice. Thanks again.

Martin Frezman
Posts: 1020
Joined: Mon Oct 31, 2016 10:05 am

Re: List files in directory

Wed Apr 12, 2017 1:55 pm

profro wrote:So it in effect will filter all names that a "." in the first index of the string? Which catches both "." and ".."

Nice. Thanks again.
Note that it also "catches" every other filename that starts with a dot, e.g., .bashrc, .profile, etc

These are the so-called "hidden" files, and, in theory, you may want to be skipping (ignoring) them, but experience tends to show that these are, in fact, the files, you care the most about. I.e., TPTB hides them from you for just that reason.
If this post appears in the wrong forums category, my apologies.

User avatar
PeterO
Posts: 5456
Joined: Sun Jul 22, 2012 4:14 pm

Re: List files in directory

Wed Apr 12, 2017 4:45 pm

Martin Frezman wrote: These are the so-called "hidden" files, and, in theory, you may want to be skipping (ignoring) them, but experience tends to show that these are, in fact, the files, you care the most about. I.e., TPTB hides them from you for just that reason.
Kindly leave your silly conspiracy theories at the door. There is no place here for them.
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

SteveSpencer
Posts: 351
Joined: Thu Mar 28, 2013 9:19 am
Location: Nottingham, UK

Re: List files in directory

Thu Apr 13, 2017 1:31 pm

rpdom wrote: Every directory has these entries except for / which only has . as it has no parent.
At the risk of seeming picky,

Code: Select all

ls -la /
does show both . and .. although I would hope that they are both links to the same thing :)

Also,
profro wrote:

Code: Select all

        char bad_1[1] = ".";
        char bad_2[2] = "..";
These are well named. The sizing element in the array should be left blank, so the compiler can work out how much space to allocate. Technically, the "string" should be terminated by a NUL character (a zero value), and a character array that isn't, would, most likely, not be handled correctly by the string(3) library routines.
Steve S
No, I can't think of anything funny that won't offend someone if they want it to...

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 24992
Joined: Sat Jul 30, 2011 7:41 pm

Re: List files in directory

Thu Apr 13, 2017 1:37 pm

Martin Frezman wrote:
profro wrote:So it in effect will filter all names that a "." in the first index of the string? Which catches both "." and ".."

Nice. Thanks again.
Note that it also "catches" every other filename that starts with a dot, e.g., .bashrc, .profile, etc

These are the so-called "hidden" files, and, in theory, you may want to be skipping (ignoring) them, but experience tends to show that these are, in fact, the files, you care the most about. I.e., TPTB hides them from you for just that reason.
So thoroughly hidden that the only way you can see them is by using the utterly obscure command

ls -a

/sarcasm off
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
“I own the world’s worst thesaurus. Not only is it awful, it’s awful."

Return to “C/C++”