reading directory - Unix

This is a discussion on reading directory - Unix ; Hi, I want read directory and print all regular files. I tried using S_ISREG, but it is not working. If I use IS_DIR it is printing all file names. here is my program ------------------------------------------------------------------- #include #include #include #include #include int ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: reading directory

  1. reading directory

    Hi,

    I want read directory and print all regular files. I tried using
    S_ISREG, but it is not working. If I use IS_DIR it is printing all file
    names.

    here is my program

    -------------------------------------------------------------------



    #include
    #include
    #include
    #include
    #include

    int main()


    {
    int c;
    DIR *dp;
    struct dirent *dirp;
    struct stat buf;
    dp=opendir("/home/hiren/new/");
    while((dirp= readdir(dp))!=NULL)
    {
    lstat(dirp->d_name,&buf);

    if(S_ISREG(buf.st_mode))
    printf("%s",dirp->d_name);
    }
    closedir(dp);

    }

    -----------------------------------------------------------------


  2. Re: reading directory

    In article <1130297913.093198.43620@g43g2000cwa.googlegroups.c om>,
    "hirenshah.05@gmail.com" wrote:

    > Hi,
    >
    > I want read directory and print all regular files. I tried using
    > S_ISREG, but it is not working. If I use IS_DIR it is printing all file
    > names.


    Someone answered this already in another newsgroup. If a question is
    *really* appropriate for multiple groups, please cross-post properly
    (post a single message with all the groups in the "Newsgroups:" line).
    However, in this case, the comp.unix.programmer group is really the only
    appropriate group, so it's not necessary to send to multiple groups at
    all.

    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***

+ Reply to Thread