using opendir() and readdir() linux commands in C... HELP! :)

Alex

Diamond Member
Oct 26, 1999
6,995
0
0
hey
i need to read a directory stored in *path and count how many files are inside it and then store this information in a structure and return it...

i cant even get opendir or readdir to work properly and the man pages just confuse me... anyone have any suggestions please?
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
What's the problem? You allocate a variable for the DIR handle, call dirhandle = opendir(string) to open the directory, if dirhandle is NULL something went wrong so you need to check errno (the man page doesn't say this, but since dirhandle would be NULL that's the only way I can figure it would set the error for you to get it), if it's not NULL you have a valid handle and can call readdir on it. readdir returns 1 dirent struct at each call, which is 1 per filename, until it hits the end and then it returns NULL. But you have to check for NULL at each call because if it has an error it'll return NULL too so you need to be prepared for NULL to not mean the entire directory has been read. So just use a while loop with readdir and an incrementing counter until you find the end of the directory.

As for storing the count in a structure, I'm not sure why you'd do that unless you have other information you're keeping in that structure too.
 

Alex

Diamond Member
Oct 26, 1999
6,995
0
0
nothinman thanks for your reply...

thing is, i dont understand what a dirent stucture is and i dont understand how to allocate the variable for the DIR handle

sorry if this is dumb... we've just plundged into this weird C course and im kinda lost...

can you please give me an example to open/read a directory stored in *path?

(i dont want you to do the question for me i would just like to understand it)

thanks for your time!

-frang


(EDIT) oh and we're supposed to store a pointer to the file and the count in teh structure thats the point of having it...
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
dirent is explained in the man page, but the #ifdefs may be confusing. Basically you end up with a structure that has members(can't remember if that's the right term or not) of:
d_ino
d_off
d_reclen
d_type
d_name.

And you could access those with as mydirecnt->d_name if you allocate mydirent as a pointer.

You should be able to allocate the DIR structure with simply "DIR *mydirhandle". Same thing with the dirent, "struct dirent *mydirent";

I just did a quick test program and it worked so I believe everything I've put here is sound =)

What is "the file", all you've mentioned so far is reading a directory.
 

Alex

Diamond Member
Oct 26, 1999
6,995
0
0
hi

ok i think i got it to work because i did what you told me to and printed out mydirent->d_name and it printed "." which is really the first item in a unix directory list eh?

so to return a list of all files in a directory i would have to stick the readdir line in a loop until mydirent == NULL ?

thanks so much for your time!
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
Well it's an entry, I wouldn't do anything that assumes "." is the first one though. FYI "." is the current directory and ".." is the parent directory.

so to return a list of all files in a directory i would have to stick the readdir line in a loop until mydirent == NULL ?

Essentially, yes. Remeber what I said about it setting mydirent to NULL on errors too.
 

Alex

Diamond Member
Oct 26, 1999
6,995
0
0
Originally posted by: Nothinman
Well it's an entry, I wouldn't do anything that assumes "." is the first one though. FYI "." is the current directory and ".." is the parent directory.

so to return a list of all files in a directory i would have to stick the readdir line in a loop until mydirent == NULL ?

Essentially, yes. Remeber what I said about it setting mydirent to NULL on errors too.

thanks!

i tried that and it returned "." ".." and every file in the directory! sweet!

im just not sure what to do if it returns null on an error.... is there any other way to find out when its done reading a directory? any how thanks for the help!
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
It returns NULL when it's done too =)

It's one of those fun things you have to discern for yourself, EBADF seems to be the only error it'll return according to the man page so you probably want to check errno (include errno.h) and if it's not EBADF then it's the end of the directory.
 

Alex

Diamond Member
Oct 26, 1999
6,995
0
0
i was about to post asking about errno but just looked it up on the man pages ;)

cool, this is shaping up nicely!

still a lot of work to do though.... :(

anyway thanks a lot dude you saved me hours of frustration!

 

zolafencer

Junior Member
Nov 3, 2012
1
0
0
Hey, i hope someone can help me. I'm acyually working on the same functionand i didn't understand well your explanation (it comes maybe from my bad english).
I'mtrying to write a program in C for linux toopen a directory and shows the file and others directories inside. I guess it's the opendir and readdir functions.
I wrote the following code after reading thoses posts to test it but it doesn't work (i expected such a result ...).
#include <dirent.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char** argv)
{
char nomFile[100];
struct DIR *dirent;
struct dirent *mydirent;
printf("Enter the directory name\n");
scanf("%s\n", nomFile);
dirent = open (nomFile);
if (dirent!= NULL)
{
while (dirent!= NULL)
{
mydirent = read (dirent->d_name);
}
}
}


Ifsomeone can explain me my mistakes. Thank you