Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
03-03-2005, 11:53 AM
|
#1
|
|
Member
Registered: Dec 2003
Location: Canada
Distribution: Suse 10
Posts: 206
Rep:
|
scan folder to search file
hi
in a c program, we need to scan a directory and find all: hist*.htm file
are there any special c function to do the job
thanks
|
|
|
|
03-03-2005, 12:02 PM
|
#2
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
Scanning a directory is implementation-dependent. Which OS and compiler are you using?
|
|
|
|
03-03-2005, 12:07 PM
|
#3
|
|
Member
Registered: Dec 2003
Location: Canada
Distribution: Suse 10
Posts: 206
Original Poster
Rep:
|
Quote:
Originally posted by itsme86
Scanning a directory is implementation-dependent. Which OS and compiler are you using?
|
we use linux and gcc (ppc)
|
|
|
|
03-03-2005, 01:12 PM
|
#4
|
|
Member
Registered: Dec 2003
Location: Canada
Distribution: Suse 10
Posts: 206
Original Poster
Rep:
|
the file i need to search are: hist_2005-02-24.htm..
hist_0000-00-00.htm
0 can be any number
i begining to write this code
Code:
int searchfile(char *history_dir, char *filename){
DIR *pdir;
pdir = opendir(history_dir);
struct dirent *pent;
if (!pdir){
printf ("%s - Incapable d'utiliser opendir()", strerror (errno));
return 1;
}
while ((pent=readdir(pdir))!=NULL){
printf("%s", pent->d_name);
}
closedir(pdir);
return 0;
}
|
|
|
|
03-03-2005, 01:25 PM
|
#5
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
You can do something like:
Code:
if(strncmp(pent->d_name, "hist", 4) == 0 && strcmp(pent->d_name+15, ".htm") == 0)
{
// It's a match
}
|
|
|
|
03-03-2005, 01:26 PM
|
#6
|
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530
Rep: 
|
I have made this some time ago.
I don't know if that's what you need, but it scans down sub-directories.
Change the line: #define FILENAME_FILTER "*.txt" to the shell pattern as you need.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <fnmatch.h>
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>
#define FILENAME_FILTER "*.txt"
int scandirectory(const char *dirname)
{
DIR *dir;
struct dirent *entry;
char path[PATH_MAX];
dir = opendir(dirname);
if (dir == NULL) {
perror("Error opendir()");
return 0;
}
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_DIR) {
if (strcmp(entry->d_name, ".")
&& strcmp(entry->d_name, "..")) {
snprintf(path, (size_t) PATH_MAX, "%s/%s", dirname,
entry->d_name);
scandirectory(path);
}
} else if (entry->d_type == DT_REG) {
if (!fnmatch(FILENAME_FILTER, entry->d_name, 0)) {
/*
* This is the place where a file matching the filter is
* found. Do file processing here, in this case, just print.
*/
printf("%s/%s\n", dirname, entry->d_name);
}
}
}
closedir(dir);
return 1;
}
int main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "Usage:\t%s <directory>\n", *argv);
return 1;
}
return !scandirectory(argv[1]);
}
Last edited by Hko; 03-03-2005 at 01:35 PM.
|
|
|
|
03-03-2005, 01:33 PM
|
#7
|
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530
Rep: 
|
-- Accidental double post.
-- Please ignore/remove.
|
|
|
|
03-03-2005, 01:37 PM
|
#8
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
Huh, I never came across fnmatch() before. That's cool.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 05:53 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|