Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

directory listing

Posted on 2004-03-21
7
Medium Priority
?
538 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

722 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