Solved

directory listing

Posted on 2004-03-21
7
532 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
  • 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
Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

856 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