Solved

Accessing directory listing.

Posted on 1998-11-30
2
269 Views
Last Modified: 2010-04-21
How can I access the directory listing using system calls? I wanted to write a program when given a directory name prints all the files in it?
0
Comment
Question by:vividh
[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
2 Comments
 
LVL 2

Expert Comment

by:seedy
ID: 2008151
Use opendir, readdir, closedir and other related routines.  These are part of the standard library routines.  Here is a sample code that prints the names  of  all  the files in the current directory: (taken from the man pages)

#include <stdio.h>
#include <dirent.h>

main()
{
       DIR *dirp;
       struct dirent *direntp;

       dirp = opendir( "." );
       while ( (direntp = readdir( dirp )) != NULL )
               (void)printf( "%s\n", direntp->d_name );
       (void)closedir( dirp );
       return (0);
}

Some flavors of UNIX may also have getdents or getdirentries; read man pages for more info.

0
 

Accepted Solution

by:
rajeevm earned 100 total points
ID: 2008152
Can you please try ls -R option , and if you want to write a program , then use system function and invoke the above command .
I think it will solve your problem
Thanks & Regards,
Rajeev Mishra
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

717 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