|
Home > Archive > Unix Programming > March 2005 > Search txt file and put name in a ...
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Search txt file and put name in a ...
|
|
| collinm 2005-03-24, 7:53 am |
| i try to search txt file in a folder
my code
Generic Code:
int searchfile(char *dir, char *filename, int flength, char *ext)
{
DIR *pdir;
pdir = opendir(dir);
struct dirent *pent;
if (!pdir)
{
printf ("%s - Incapable d'utiliser opendir()\n", strerror
(errno));
return 1;
}
while((pent = readdir(pdir)) != NULL)
{
if(strcmp(pent->d_name + flength, ext) == 0)
{
printf("%s\n", pent->d_name);
}
}
closedir(pdir);
return 0;
}
i call this function with
Generic Code:
searchfile(local_dir_led,"", 28, ".txt");
all my file name are the same size... but will not be true soon
how to do it when the file name size are not the same?
i would like to put the file name in a list....
what is the better way to do it?
| |
| Barry Margolin 2005-03-25, 2:48 am |
| In article <1111672094.492840.84850@o13g2000cwo.googlegroups.com>,
"collinm" <collinm@laboiteaprog.com> wrote:
> i try to search txt file in a folder
>
> my code
>
>
> Generic Code:
>
> int searchfile(char *dir, char *filename, int flength, char *ext)
> {
> DIR *pdir;
> pdir = opendir(dir);
> struct dirent *pent;
> if (!pdir)
> {
> printf ("%s - Incapable d'utiliser opendir()\n", strerror
> (errno));
> return 1;
> }
>
> while((pent = readdir(pdir)) != NULL)
> {
> if(strcmp(pent->d_name + flength, ext) == 0)
> {
> printf("%s\n", pent->d_name);
> }
> }
> closedir(pdir);
> return 0;
> }
>
>
> i call this function with
>
> Generic Code:
>
> searchfile(local_dir_led,"", 28, ".txt");
>
>
>
> all my file name are the same size... but will not be true soon
> how to do it when the file name size are not the same?
Instead of passing flength as a parameter, compute it:
flength = strlen(pent->d_name) - strlen(ext);
If flength < 0 then the filename is shorter than the extension, so skip
this file.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
|
|
|
|
|