How to read a directory in a C program?

Answers were Sorted based on User's Feedback



How to read a directory in a C program? ..

Answer / lakshmi sreekanth

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>

main()
{
struct dirent *dp;
DIR *dfd = opendir("/home/sreekanth");

if(dfd != NULL)
{
while((dp = readdir(dfd)) != NULL)
printf("%s\n", dp->d_name);
closedir(dfd);
}
return 0;
}

Is This Answer Correct ?    14 Yes 4 No

How to read a directory in a C program? ..

Answer / muthu

please write the correct program with output...

by prof.muthu m.c.a phd etc..
ph:9962940220

Is This Answer Correct ?    6 Yes 4 No

How to read a directory in a C program? ..

Answer / rakesh

void main()
{
char path[100];
cout<<"Enter the path of directory::";
cin.getline(path,100);
system("cd path");
system("dir");
} // In windows
// For linux change dir to ls.

Is This Answer Correct ?    10 Yes 15 No

How to read a directory in a C program? ..

Answer / prince

main()
{
char path[100];
printf("Enter the path of directory:");
scanf("%s",path);
system("cd path");
system("ls");
}

Is This Answer Correct ?    2 Yes 20 No

Post New Answer

More C Code Interview Questions

How to count a sum, when the numbers are read from stdin and stored into a structure?

1 Answers  


How will you print % character? a. printf(“\%”) b. printf(“\\%”) c. printf(“%%”) d. printf(“\%%”)

4 Answers   HCL,


int a=1; printf("%d %d %d",a++,a++,a); need o/p in 'c' and what explanation too

1 Answers  


String reverse with time complexity of n/2 with out using temporary variable.

10 Answers   NetApp, Symantec,


main() { main(); }

1 Answers  






To reverse an entire text file into another text file.... get d file names in cmd line

0 Answers   Subex,


¦void main() ¦{ ¦int i=10,j; ¦ j=i+++i+++i; ¦printf("%d",j); ¦getch(); ¦} ¦ output:-30 but in same question if we write as- ¦void main() ¦{ ¦int i=10; ¦ int j=i+++i+++i; ¦printf("%d",j); ¦getch(); ¦} ¦ output:-33 why output is changed from 30 to 33. Can any body answer...

3 Answers  


main() { while (strcmp(“some”,”some\0”)) printf(“Strings are not equal\n”); }

1 Answers  


main() { char *p = “ayqm”; char c; c = ++*p++; printf(“%c”,c); }

1 Answers  


#include<stdio.h> main() { struct xx { int x=3; char name[]="hello"; }; struct xx *s; printf("%d",s->x); printf("%s",s->name); }

3 Answers   Hexaware,


main() { unsigned int i=10; while(i-->=0) printf("%u ",i); }

2 Answers   HP,


#include <stdio.h> #define a 10 main() { #define a 50 printf("%d",a); }

2 Answers  


Categories