On reading Directory contents and listing them.

This is a discussion on On reading Directory contents and listing them. within the C Programming forums, part of the General Programming Boards category; Hello everyone, Been having fun reading through the many interesting tutorials here, and decided to ask directly on something. Just ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    4

    On reading Directory contents and listing them.

    Hello everyone,

    Been having fun reading through the many interesting tutorials here, and decided to ask directly on something.

    Just practising with the code of reading in files of a directory, and printing out.
    I want the code to always read the files only stored in the directory rib_seqfiles. The problem is that when it prints out the output, it correctly prints all the files in the folder, but also prints out . and ..

    Heres the code:-

    inser
    Code:
    struct dirent *pfileList;
    DIR *pDIR;
    pDIR = opendir("rib_seqfiles");
    
        if ( pDIR == NULL )
        {
          printf("The directory you attempting to open == NULLls");  exit(1);
         }
    
    
           pfileList = readdir( pDIR ); 
           while ( pfileList != NULL )
            {
                    printf( "%s\n", pfileList->d_name );
                    pfileList = readdir( pDIR );
             }
    Heres an example of the output:-

    example2.rib
    .
    example1.rib
    lahma.rib
    lahma (3rd copy)
    ..

    In case this matters, the reason I don't want . and .. printed out is that in the while loop I plan to run a function that takes the filename as an argument and does some stuff to it, and I want to do this for every file in the folder.

    Thanks in advance,

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    983
    Just compare each entry to "." and "..". If it matches either of those, ignore it.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    4
    Ah ok, the idea occurred to me. but I wasn't sure if there was some command thats part of the language that would ignroe those.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Deathfrost View Post
    Ah ok, the idea occurred to me. but I wasn't sure if there was some command thats part of the language that would ignroe those.
    You have misunderstood something

    Commands that would ignore "." and ".." are things that exist in other languages, where the compiler or interpreter are written in C. But sometimes not -- I guess this is a toss up since it could not be much more than a 25 character line in any language.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Comparing the filename pfileList->d_name returned from readdir() to "." and ".." is okay if those are the only directories there. But if there are other directories whose names are not known in advance, an alternative is to stat() the filename and see if it is a regular file (S_IFREG) or not. This way all directories can be excluded from the list.

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    4
    Quote Originally Posted by itCbitC View Post
    Comparing the filename pfileList->d_name returned from readdir() to "." and ".." is okay if those are the only directories there. But if there are other directories whose names are not known in advance, an alternative is to stat() the filename and see if it is a regular file (S_IFREG) or not. This way all directories can be excluded from the list.
    There shouldn't be any other directories inside, but I suppose in the interest of robustness, that sounds like the better way of doing it. Anyway using the following code didn't work:-
    Code:
    pfileList = readdir( pDIR );
    while ( pfileList != NULL ) 
     
     {   
    if(pfileList->d_name =="." || pfileList->d_name=="..") //checks to see if the current file read in pfileList is not "." or ".." 
     {
      pfileList =readdir(pDIR);
     }        
     else
       {
     char *str1=commandName;
     char *str2=pfileList->d_name;
     printf("\n\nfilename name is %s",str2);
     pfileList = readdir( pDIR );
      }
    }
    It still prints out "." and "..", this is just the related section, I have all the variables declared.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Since when has == been able to compare strings? (Hint: never.) (Double hint: There is a "string compare" function strcmp.)

  8. #8
    Registered User
    Join Date
    Jul 2009
    Posts
    4
    Quote Originally Posted by tabstop View Post
    Since when has == been able to compare strings? (Hint: never.) (Double hint: There is a "string compare" function strcmp.)
    Haha, sorry I just figured out that == is not comparing two strings. Am I correct in saying its comparing 2 pointers? Yeah got it to work thanks. This is only my 2nd program in C ever, and I'm having a hard time keeping straight what is pointing to what.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by Deathfrost View Post
    Haha, sorry I just figured out that == is not comparing two strings. Am I correct in saying its comparing 2 pointers?...
    Yep! it's comparing the two pointers.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    . . . which are extremely unlikely to ever be the same. Reason being, the ".." string literals are, well, string literals. The compiler will probably collect all occurrences of the same string literal and make them actually refer to the same string, but I doubt it would do this between files. I don't know much about readdir() and friends, but it probably doesn't use a string literal to represent "." and ".." anyway (I'd imagine it would either dynamically allocate the memory or use a buffer inside the structure). So the pointer comparisons would probably always fail.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying an entire directory
    By JustMax in forum C Programming
    Replies: 3
    Last Post: 03-11-2009, 04:06 PM
  2. Using sys/stat.h and listing directory contents in C...
    By smoothdogg00 in forum C Programming
    Replies: 2
    Last Post: 03-14-2006, 01:11 AM
  3. Help Required!!!!!
    By bobthebullet990 in forum C Programming
    Replies: 14
    Last Post: 11-27-2005, 02:56 PM
  4. Directory listing in treeview control
    By Hunter2 in forum Windows Programming
    Replies: 1
    Last Post: 06-23-2003, 04:52 PM
  5. Displaying disk contents in window
    By HisWord in forum Windows Programming
    Replies: 6
    Last Post: 08-15-2002, 08:31 PM

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