Search txt file and put name in a ...

C

collinm

i try to search txt file in a folder

my code

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
Code:
searchfile(local_dir_led,"", 28, ".txt");


all my file name are the same size...
they are surely a better way to do it (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?
 
D

Default User

collinm said:
i try to search txt file in a folder

my code

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;
}[/QUOTE]


Lots of this stuff is off-topic, but your question may not be. For any
actual questions involving the above, you'll need comp.unix.programmer.
[QUOTE]

i call this function with
Code:
searchfile(local_dir_led,"", 28, ".txt");

That doesn't sound good. Magic numbers are usually very poor practice.

all my file name are the same size...

they are surely a better way to do it (the file name size are not the
same)...

Sure.

const char *extension = ".txt";
size_t len;
len = strlen (filename) - strlen (extension);

That gives you the length of the base filename. I wouldn't even bother
passing that into the function, just have searchfile compute it.

i would like to put the file name in a list....
what is the better way to do it?

What kind of list? Linked list? Expandable array? Without knowing what
you intend to do with them it's hard to advise.



Brian
 
C

CBFalconer

collinm said:
my code

Code:
int searchfile(char *dir, char *filename, int flength, char *ext)[/QUOTE]

syntax error [code]
[QUOTE]
{
DIR *pdir;[/QUOTE]

undefined DIR
[QUOTE]
pdir = opendir(dir);[/QUOTE]

undefined opendir
[QUOTE]
struct dirent *pent;[/QUOTE]

undefined, struct dirent.  Also illegal here after executable code.
[QUOTE]
if (!pdir)
{
printf ("%s - Incapable d'utiliser opendir()\n", strerror(errno));[/QUOTE]

undefine printf.  This is standard in stdio.h.
[QUOTE]
return 1;
}

while((pent = readdir(pdir)) != NULL)[/QUOTE]

undefined readdir and NULL.  NULL is also defined in stdio.h
[QUOTE]
{
if(strcmp(pent->d_name + flength, ext) == 0)[/QUOTE]

undefined strcmp.  Standard in string.h
undefined d_name
[QUOTE]
{
printf("%s\n", pent->d_name);[/QUOTE]

Undefined printf.  See above.
Undefined d_name
[QUOTE]
}
}
closedir(pdir);[/QUOTE]

undefined closedir
[QUOTE]
return 0;
}[/QUOTE]

All the errors I could find.  Probably more.  Can't be compiled.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top