Solved

Accessing directory listing.

Posted on 1998-11-30
2
267 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
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

Technology Partners: 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

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
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 navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

730 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