Thread: opendir failure

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    opendir failure

    [IGNORE THIS POST]

    In my previous thread (http://cboard.cprogramming.com/showthread.php?t=104964) I was trying to find out why opendir would sometimes fail in a script which recurses thru a directory tree. No one could tell me so I wrote a simple test:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <dirent.h>
    #include <errno.h>
    
    int main (int argc, char *argv[]) {
            struct dirent *dcon;
            DIR *dir = opendir (argv[1]);
            if (dir == NULL) { 
                    printf ("&#37;s: %s\n", argv[1], strerror(errno)); 
                    exit(1);
            }
                
            while (dcon = readdir (dir)) {
                    puts (dcon->d_name);
            }       
    }
    Low and behold, this fails on every fourth or five directory i try it on, regardless of the pathname, length, ownership, or permissions (the error is "No such file or directory"). Since zacs7 tested the other script and got the same result, it can't be filesystem corruption...however we both use linux/GNU and (I presume) the GNU C libraries.

    Can anyone explain the problem?
    Last edited by MK27; 07-10-2008 at 11:44 AM. Reason: personal failures

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    So, just to make sure we're on the same page...you're running this test program repeatedly with different directories passed as command line arguments and it's failing every four or five times?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Also, what actual value are you passing for argv[1]
    Or are you passing an argument at all?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    You should check your return value of your opendir(). Also main() should return an int for correct coding practices.

    Opendir() will open upto at least OPEN_MAX files and directories.

    http://www.opengroup.org/onlinepubs/...h/opendir.html

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    I have made a horrible mistake...

    and i'm sorry sorry sorry

    Never take MK27 serious again.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    That's odd, my man page [2007-07-30] doesn't mention that (Which I thought was the problem).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 04:47 AM
  2. Integer Emulation
    By Elysia in forum C++ Programming
    Replies: 31
    Last Post: 03-18-2008, 01:03 PM
  3. php: glob() vs opendir() and friends
    By MisterSako in forum Tech Board
    Replies: 1
    Last Post: 03-16-2006, 01:42 AM
  4. CreateWindow failure
    By TheDan in forum Windows Programming
    Replies: 6
    Last Post: 07-08-2005, 07:49 AM
  5. Am i a Failure?
    By Failure!!! in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 05-28-2003, 11:50 AM