Solved

Searching Files in a directory

Posted on 1998-11-18
6
190 Views
Last Modified: 2010-04-15
What is the C function is used to search for files in the directory or subdirectory? Theare is a function ' stat ' for which if you give the file name we can get the information about that file whether it is ordinary file or a directory etc. Like this is there any function which searchs for the files in the directory?
0
Comment
Question by:jsreedhar
  • 3
  • 2
6 Comments
 

Author Comment

by:jsreedhar
ID: 1254407
Let me know the answer as early as possible.
Thank you.
0
 

Author Comment

by:jsreedhar
ID: 1254408
Let me know the answer as early as possible,
Thank you.
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1254409
1.  What operating system are you using?
2.  What exactly do you mean by "search"?  Determining if a filename exists?  Listing all files that meet certain criteria?  Listing all files in a given directory?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Accepted Solution

by:
rbr earned 100 total points
ID: 1254410
Use opendir to open the directory structur and readdir to read ony entry. with stat (fstat) you can check the type of the entry (file or subdir).
0
 
LVL 10

Expert Comment

by:rbr
ID: 1254411
DIR *pdir
struct dirent *pentry;
struct stat stat;

pdir=opendir ("/");
while (NULL != (pentry = readdir (pdir))) {
      stat (pdir->d_name,&stat);
      if (stat.st_mode & S_IFDIR) {
            printf ("Directory: %s",pdir->d_name);
      }
}

0
 

Author Comment

by:jsreedhar
ID: 1254412
I am sorry, If I am not clear in the question. The question is like this. If I give a pattern say
' *.* ', it should list all the file names in the current directory and the sub-directories. Like in windows sdk programming we have FindFirstFile and FineNextFile which helps for doing the above. Same way is there any functions in C language. Actually I want write for both dos and unix. That is why I am using only C functions.

Thank you.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question