22 September, 2010

C Program to show the contents of a directory

This is a C program to show directory contents in a Unix machine

#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<string.h>
#include<dirent.h>
           
main()
{
    struct dirent *p;
    char a[50];
    printf("\nEnter the directory ---------------->: \n");
    scanf("%s",a);
    DIR *f=opendir(a);
    printf("\n---------------directory contents are ----------------- \n");
     while((p=readdir(f))!=NULL)
    {
           printf("%s\n",p->d_name);
    }
    closedir(f);
}

Warning : program is written using system calls in Unix

1 comments:

compizspec on Wednesday, September 22, 2010 said...

i have tried it,its coool
pls include an error msg is dir is not found
spec@spec-gear:~$ ./a.out

Enter the directory ---------------->:
spec

---------------directory contents are -----------------
Segmentation fault

Post a Comment

speak out... itz your time !!!

 

Terminal Diary | techblog Copyright © 2009 Gadget Blog is Designed by jintu jacob Powered by Blogger