Solved

directory listing

Posted on 2004-03-21
7
528 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
Comment Utility
. 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
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:shepson990
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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 opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now