opendir failure

This is a discussion on opendir failure within the C Programming forums, part of the General Programming Boards category; [IGNORE THIS POST] In my previous thread ( ) I was trying to find out why opendir would sometimes ...

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    segmentation fault

    opendir failure


    In my previous thread (directory operations, linux) 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:

    #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)); 
            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 12:44 PM. Reason: personal failures

  2. #2
    Join Date
    Dec 2007
    So, just to make sure we're on the same'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
    The edge of the known universe
    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
    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.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    segmentation fault

    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
    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, 02: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, 08:49 AM
  5. Am i a Failure?
    By Failure!!! in forum A Brief History of
    Replies: 16
    Last Post: 05-28-2003, 12:50 PM

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