Search txt file and put name in a ...
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Search txt file and put name in a ...




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Search txt file and put name in a ...  
collinm


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-24-05 12:53 PM

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?






[ Post a follow-up to this message ]



    Re: Search txt file and put name in a ...  
Barry Margolin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-25-05 07: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 ***





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 05:00 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum 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
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register