Saturday, July 30

Display the Contents of a Directory





    Here is a simple program to open and display the contents of a directory.
                                             

 DIRECTORY.C

/*
PROGRAM NAME:DIRECTORY.C
AIM:TO DISPLAY THE CONTENTS OF A DIRCTORY
DATE:07/30/2011
*/
#include<stdio.h>
#include<dirent.h>

main()
{
char dirname[10];
DIR *ptr;
struct dirent *dir;
printf("Enter the directory Name..");
scanf("%s",dirname);
ptr=opendir(dirname);

printf("\nContents in %s are..\n",dirname);
while((dir=readdir(ptr))!=NULL)
printf("%s\n",dir->d_name);
getch();
closedir(ptr);
}



Note

  • System calls for directory operations are included in the header file <dirent.h>.
  • opendir: Opens afile directory.
  • closedir: Closes a directory.
  • readdir : Reads the next record from file.
  • rewinddir: Sets file pointer to the beginning of file.




0 comments :

Post a Comment