Solved

directory listing

Posted on 2004-03-21
7
534 Views
Last Modified: 2010-04-15
hi

i have the code below which prints the contents of a directory specified in dp.

main()
{
        DIR *dp = opendir(".");
        struct dirent *d;

        while ((d = readdir(dp)) != NULL)
        {
                printf("%s\n", d->d_name);
        }
 
        closedir(dp);

when it prints the contents it will also print a "." and "..". So i want to  be able to do something like the following, so it doesnot print ".,;". but his does not work. Any ideas

       while ((d = readdir(dp)) != NULL)
        {
               while(strpbrk(d->d_name,".,;"){
                        printf("%s\n", d->d_name);
               }
        }

        closedir(dp);



}
0
Comment
Question by:shepson990
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10646677
. and .. are the only two special directories that you are likely to encounter ...
. means current directory
.. means parent directory

        while ((d = readdir(dp)) != NULL)
        {
                if ( strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") )
                          printf("%s\n", d->d_name);
        }
0
 

Author Comment

by:shepson990
ID: 10646759
how would i get the following to work
while ((d = readdir(dp)) != NULL)
{
      if ( strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") )
        sprintf(szBuf2,"/home/shepson/netstatus/errors/%s",d->d_name);
      read=fopen(szBuf2,"r");          
      printf("%s\n", read);
}
 
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10646781
sprintf(szBuf2,"/home/shepson/netstatus/errors/%s",d->d_name);
     read=fopen(szBuf2,"r");          
     printf("%s\n", read);

fopen returns a file handle !!! Why would you like to print the file handle ? What are you trying to do ? Also if fopen fails, it will return NULL and you will try to print NULL ?
Another thing, read is a system call and be careful when you use it as a variable name
Since you may not be interested in opening . and .., you can include the fopen in the if block

while ((d = readdir(dp)) != NULL)
{
     if ( strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") )
     {
          sprintf(szBuf2,"/home/shepson/netstatus/errors/%s",d->d_name);
          read=fopen(szBuf2,"r");          
     }
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:shepson990
ID: 10646803
basicly i want to be able to open and print each file returned in the directory listing.
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 250 total points
ID: 10646815
char buf [128];

while ((d = readdir(dp)) != NULL)
{
     if ( strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") )
     {
          sprintf(szBuf2,"/home/shepson/netstatus/errors/%s",d->d_name);
          fp =fopen(szBuf2,"r");          
          if ( fp == NULL )
                  printf ( "error opening file %s\n", d->d_name );
          else
          {
                  printf ( "printing file %s\n", d->d_name );
                 while ( fgets ( buf, 128, fp ) )
                          printf ( "%s", buf );
                 fclose (fp);
           }
     }
}
0
 

Author Comment

by:shepson990
ID: 10646846
thanks. Just one question, why do you have to use fgets to get the contents on the file
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10646853
fopen simply opens the file and gives you the handle for the open file ... next you need to use that handle to read from the file ... that is exactly what fgets does ...

reads a line from the open file
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question