Go Back   AnandTech Forums > Software > Software for Windows

Forums
· Hardware and Technology
· CPUs and Overclocking
· Motherboards
· Video Cards and Graphics
· Memory and Storage
· Power Supplies
· Cases & Cooling
· SFF, Notebooks, Pre-Built/Barebones PCs
· Networking
· Peripherals
· General Hardware
· Highly Technical
· Computer Help
· Home Theater PCs
· Consumer Electronics
· Digital and Video Cameras
· Mobile Devices & Gadgets
· Audio/Video & Home Theater
· Software
· Software for Windows
· All Things Apple
· *nix Software
· Operating Systems
· Programming
· PC Gaming
· Console Gaming
· Distributed Computing
· Security
· Social
· Off Topic
· Politics and News
· Discussion Club
· Love and Relationships
· The Garage
· Health and Fitness
· Merchandise and Shopping
· For Sale/Trade
· Hot Deals with Free Stuff/Contests
· Black Friday 2014
· Forum Issues
· Technical Forum Issues
· Personal Forum Issues
· Suggestion Box
· Moderator Resources
· Moderator Discussions
   

Reply
 
Thread Tools
Old 01-19-2004, 06:18 PM   #1
Alex
Diamond Member
 
Join Date: Oct 1999
Posts: 6,995
Default using opendir() and readdir() linux commands in C... HELP! :)

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?
__________________
ANAHEIM DUCKS 2007 STANLEY CUP CHAMPIONS!
Alex is offline   Reply With Quote
Old 01-19-2004, 06:38 PM   #2
Nothinman
Elite Member
 
Nothinman's Avatar
 
Join Date: Sep 2001
Posts: 30,672
Default RE: using opendir() and readdir() linux commands in C... HELP! :)

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.
__________________
http://www.debian.org
Nothinman is offline   Reply With Quote
Old 01-19-2004, 06:44 PM   #3
Barnaby W. Fi
Elite Member
 
Join Date: Aug 2001
Posts: 12,343
Default RE:using opendir() and readdir() linux commands in C... HELP! :)

Quote:
Originally posted by: Nothinman
What's the problem?
Barnaby W. Fi is offline   Reply With Quote
Old 01-19-2004, 06:48 PM   #4
Alex
Diamond Member
 
Join Date: Oct 1999
Posts: 6,995
Default RE:using opendir() and readdir() linux commands in C... HELP! :)

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...
__________________
ANAHEIM DUCKS 2007 STANLEY CUP CHAMPIONS!
Alex is offline   Reply With Quote
Old 01-19-2004, 07:02 PM   #5
Nothinman
Elite Member
 
Nothinman's Avatar
 
Join Date: Sep 2001
Posts: 30,672
Default RE: using opendir() and readdir() linux commands in C... HELP! :)

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.
__________________
http://www.debian.org
Nothinman is offline   Reply With Quote
Old 01-19-2004, 07:12 PM   #6
Alex
Diamond Member
 
Join Date: Oct 1999
Posts: 6,995
Default RE:using opendir() and readdir() linux commands in C... HELP! :)

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!
__________________
ANAHEIM DUCKS 2007 STANLEY CUP CHAMPIONS!
Alex is offline   Reply With Quote
Old 01-19-2004, 07:21 PM   #7
Nothinman
Elite Member
 
Nothinman's Avatar
 
Join Date: Sep 2001
Posts: 30,672
Default RE: using opendir() and readdir() linux commands in C... HELP! :)

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.

Quote:
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.
__________________
http://www.debian.org
Nothinman is offline   Reply With Quote
Old 01-19-2004, 07:30 PM   #8
Alex
Diamond Member
 
Join Date: Oct 1999
Posts: 6,995
Default RE:using opendir() and readdir() linux commands in C... HELP! :)

Quote:
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.

Quote:
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!
__________________
ANAHEIM DUCKS 2007 STANLEY CUP CHAMPIONS!
Alex is offline   Reply With Quote
Old 01-19-2004, 07:34 PM   #9
Nothinman
Elite Member
 
Nothinman's Avatar
 
Join Date: Sep 2001
Posts: 30,672
Default RE: using opendir() and readdir() linux commands in C... HELP! :)

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.
__________________
http://www.debian.org
Nothinman is offline   Reply With Quote
Old 01-19-2004, 07:46 PM   #10
Alex
Diamond Member
 
Join Date: Oct 1999
Posts: 6,995
Default RE:using opendir() and readdir() linux commands in C... HELP! :)

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!

__________________
ANAHEIM DUCKS 2007 STANLEY CUP CHAMPIONS!
Alex is offline   Reply With Quote
Old 11-03-2012, 10:27 AM   #11
zolafencer
Junior Member
 
Join Date: Nov 2012
Posts: 1
Default

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
zolafencer is offline   Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 01:06 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.