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 ]
|