I'm trying to add all the files in the current directory to my implemented archive. What function(s) could I use to access all these files? After doing some research online and in the man pages, all I have found is simple I/O like read, write, close, etc.
Asked
Active
Viewed 1,483 times
3 Answers
0
This web page seems to have what you want.
http://www.gnu.org/software/libc/manual/html_mono/libc.html#Opening-a-Directory

Marichyasana
- 2,966
- 1
- 19
- 20
0
You can try with this.
main() {
DIR *d;
struct dirent *e;
e=malloc(sizeof(struct dirent));
d=opendir("<your_directory_name>");
while ((e = readdir(d)) != NULL) {
printf("%d %s\n", e->d_type, e->d_name);
}
closedir(d);
}

Alex Reynolds
- 95,983
- 54
- 240
- 345

Manik Sidana
- 2,005
- 2
- 18
- 29
-
1Don't call `malloc()`, that is a leak -- the result is overwritten. – Dietrich Epp Oct 22 '12 at 04:25
-
It will be followed by a free call. The intention was to demonstrate readdir() call..... – Manik Sidana Oct 22 '12 at 09:40
-
That won't fix anything, since at that point `e == NULL`. **The call to `malloc()` is wrong.** – Dietrich Epp Oct 22 '12 at 12:30