readdir() gives "." and ".." and doesn't exclude some folders

This is a discussion on readdir() gives "." and ".." and doesn't exclude some folders within the C Programming forums, part of the General Programming Boards category; I was trying to make an interactive directory menu. But in the directory where he has to read, there are ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    11

    readdir() gives "." and ".." and doesn't exclude some folders

    I was trying to make an interactive directory menu.
    But in the directory where he has to read, there are a few directories that i don't want my menu to show and the "." and ".." still shows to...

    I just changed all my specific names so you can modify it easier...

    please help me, and here is my code..

    Code:
    #include <stdio.h>
    #include <errno.h>
    #include <sys/types.h>
    #include <dirent.h>  
    
    #define dir getenv("HOME")
    
    main()
    {
    DIR *dip;
    struct dirent *dit;
    int i = 1;
    int n = 0;
    char list_dir[20];
    char* list_var[8];
    
    dip = opendir(dir);
      if (dip == NULL)
        {
          perror("opendir");
          return(CODE);
        }
      while (( dit = readdir(dip)) != NULL)
        {
        start:
        sprintf(list_dir,"%s",dit->d_name);
        if (list_dir = "bad_dir"){goto start;}
        if(list_dir == "."){goto start;}
        if(list_dir == ".."){goto start;}
        printf("\t\t\t(%d) %s\n",i,list_dir);
        list_var[n] = list_dir;
        i++;
        n++;
        }
      if (closedir(dip) == -1)
        {
          perror("closedir");
          return 0;
        }
    }
    at the end there was a switch for the choice list and list_var[n] was a variable that takes the the name of the dir chosen, for further use...

    But the problem here...
    1 -> it still doesn't exclude the "." ".." and "bad_dir" from my list
    2 -> my list_var always give the last dir in my case always the ".." :-(

    Please give me some help i've changed this code 100 times but can't find a good solution.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    == has never and will never compare strings. strcmp compares strings.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    And
    Code:
    if (list_dir = "bad_dir"){goto start;}
    has never and will never give you the comparison you want.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,262
    Also, your use of goto has given you an endless loop. What you really want instead of "goto start" is "continue". The continue statement will take you back to the beginning of the loop (like your goto), and it will run the evaluation statement again (readdir) which is what you need to avoid an endless loop.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    11
    yes... :-D

    this is my first program i'm writing and it takes a long time... certainly because i still have to learn a lot...
    Thanks for all the reply's and answers...

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21