Reading directories with Linux in C

Reading directories with Linux in C

Post by Dick Thornto » Mon, 10 Feb 1997 04:00:00



Can someone help me with reading a direcory in C under Linux? I have the Redhat version. Several books I have
show that reading a directory is same as any other file, at least if it is opened for reading only. When I
try, the open works o.k., but I get no data with a read, and if I issue a perror() after the read, the
returned message says the file I am trying to read is a directory. I know that, of course, and I would like to
have it return the data to me. I have tried using stream I/O and the older I/O method with no luck. And I'm
using simple programs that are given in the books I have.

Thanks in advance,

Dick Thornton

 
 
 

Reading directories with Linux in C

Post by A. Baner » Tue, 11 Feb 1997 04:00:00


: Can someone help me with reading a direcory in C under Linux? I have the Redhat version. Several books I have
: show that reading a directory is same as any other file, at least if it is opened for reading only. When I
: try, the open works o.k., but I get no data with a read, and if I issue a perror() after the read, the
: returned message says the file I am trying to read is a directory. I know that, of course, and I would like to
: have it return the data to me. I have tried using stream I/O and the older I/O method with no luck. And I'm
: using simple programs that are given in the books I have.

: Thanks in advance,

:* Thornton

man opendir()

 
 
 

Reading directories with Linux in C

Post by Avery Lodat » Tue, 11 Feb 1997 04:00:00



> Can someone help me with reading a direcory in C under Linux? I have the Redhat version. Several books I have
> show that reading a directory is same as any other file, at least if it is opened for reading only. When I
> try, the open works o.k., but I get no data with a read, and if I issue a perror() after the read, the
> returned message says the file I am trying to read is a directory. I know that, of course, and I would like to
> have it return the data to me. I have tried using stream I/O and the older I/O method with no luck. And I'm
> using simple programs that are given in the books I have.

> Thanks in advance,

>* Thornton

man opendir

should get you started.

 
 
 

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

        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

2. How far does the system reach??

3. read a directory with read()

4. Linux and ipx

5. Sherlock linux plug-ins / sherlock for linux?

6. Palm setup & serial port setup???

7. Redhat Linux 5.2 Install Problem "Error reading directory"

8. DHCP Cable Modem and Masquerading

9. Reading a directory (in c) in linux

10. Linux can only read small directories with 36x ATAPI CD-ROM drive

11. Multiple users read/write to same directory in Linux via Samba

12. List of plug-ins for linux/netscape please!

13. Errors compiling Wingz 1.4 Add-ins (Linux 1.3.99, GCC 2.6)