Reading a directory and knowing which type of file am I reading

Reading a directory and knowing which type of file am I reading

Post by Andres Tarall » Wed, 19 Jul 2000 04:00:00



        I'm developing a program that reads a directory (using dirent.h
functions) and if a file is a directory it displays a messagebefore the name
of the directory.

        Whoever it doesn't seems to work properly, i don't know what I 'm
doing wrong. The program lists the content of the directory but it doesn't
seem to work properly.

        This is a snippet of my program:
#include <errno.h>
#include <dirent.h>
#include <stdio.h>

int main(void){
        DIR *dir;
        struct dirent *ent;
        if (!(dir=opendir("."))) {
                perror("opendir");
                return 1;
        }

        errno=0;
        while ((ent = readdir(dir))) {
                if (ent->d_type == DT_DIR)
                        printf("DIRECTORY\t%s\n",ent->d_name);
                else
                        printf("%s\t\t%d\n",ent->d_name,ent->d_type);
                errno =0;
        }
        if (errno) {
                perror("readdir");
                return 1;
        }
        closedir(dir);
        return 0;

Quote:}

                Any idea is welcome

                                    Andres Tarallo

pD: I'm working under SuSE 6.3 english version

 
 
 

Reading a directory and knowing which type of file am I reading

Post by Tomasz Lack » Thu, 20 Jul 2000 04:00:00


Hi
I have the same experience. It seems that everytime
d_type is equal to 0 which is not equal to DT_DIR.
0 means type undefined.
I am qurious if there is a way out.
tl

>        I'm developing a program that reads a directory (using dirent.h
>functions) and if a file is a directory it displays a messagebefore the
name
>of the directory.

>        Whoever it doesn't seems to work properly, i don't know what I 'm
>doing wrong. The program lists the content of the directory but it doesn't
>seem to work properly.

>        This is a snippet of my program:
>#include <errno.h>
>#include <dirent.h>
>#include <stdio.h>

>int main(void){
>        DIR *dir;
>        struct dirent *ent;
>        if (!(dir=opendir("."))) {
>                perror("opendir");
>                return 1;
>        }

>        errno=0;
>        while ((ent = readdir(dir))) {
>                if (ent->d_type == DT_DIR)
>                        printf("DIRECTORY\t%s\n",ent->d_name);
>                else
>                        printf("%s\t\t%d\n",ent->d_name,ent->d_type);
>                errno =0;
>        }
>        if (errno) {
>                perror("readdir");
>                return 1;
>        }
>        closedir(dir);
>        return 0;
>}

>                Any idea is welcome

>                                    Andres Tarallo

>pD: I'm working under SuSE 6.3 english version


 
 
 

Reading a directory and knowing which type of file am I reading

Post by Peter Marino » Thu, 20 Jul 2000 04:00:00


For each name you get in your _read_dir_ loop you can use stat()
to get file attributes and to examine whether this is a
directory.

Peter Marinov
http://geocities.com/h2428

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

Got questions?  Get answers over the phone at Keen.com.
Up to 100 minutes free!
http://www.keen.com

 
 
 

1. I've read/am reading the faq's - but what is a Segmentation fault?

I am reading the faq's as fast as I can, but I would really appreciate a
pointer to information about Segmentation faults.  Every time I try to run
Pico (among others) the system responds with "Segmentation fault".  What
does it mean?  How can i fix it?  Is it in a FAQ?  Which one?

THANKS!!!
Dana
----------



----------
--
------------------------------------------------------------------------------
 \ The above does not represent OIT, UNC-CH, laUNChpad, or its other users. /
   ------------------------------------------------------------------------

2. Xview Error

3. read a directory with read()

4. console error message

5. LILO answers HERE read read read

6. Printing Quota

7. Read strings from one file and search for them in a directory containing htm files

8. tin on Slackware 2.0.2

9. [ak@suse.de: Re: iosched: impact of streaming read on read-many-files]

10. read() from driverfs files can read more bytes

11. ksh, read reads files funny

12. read from stdin while reading line by line a file

13. How to read from a .csv file (with read)?