put file names in array

threre were some answers to my questions till now - no one was working
i need it for "c" program in windows!!!!!!!!

i get a  path of a directory called
"path_files" in a string
the function should read all the *.wrl files in the folder and put the names of the wrl files in an array of strings called
char **filebd;
i dont know how many files are in the folder.
i need to count them and put them in int - NO_OF_FILES
i need the function from the beginning including:
1)pointers declerations
2)malloc of the array of strings
3)making loop on all files and putting all the names in the array
4)especially 3!!!!!!


smbdy wrote

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>

int main(){
    DIR* d;
    size_t cur_size=0;
    size_t max_size=12;
    char** dirlist=(char**)malloc(max_size*sizeof(char*));
    struct dirent* dentry;
    size_t namelen;
    int i;
   
    if(!(d=opendir("."))){
        fprintf(stderr,"Cannot open dir '.'");
        exit(0);
    }
   
    while((dentry=readdir(d))!=NULL){
        namelen = strlen(dentry->d_name);
       
        if(strcmp(&dentry->d_name[namelen-4],".wrl"))
            continue; /* Skip entry */
       
        dirlist[cur_size] = (char*) malloc(namelen+1);
        strcpy(dirlist[cur_size],dentry->d_name);
       
        cur_size++;
        if(cur_size>=max_size){
            max_size *= 2;
            dirlist=(char**)realloc(dirlist,max_size*sizeof(char*));
        }
    }
   
    closedir(d);
    for(i=0;i<cur_size;i++)
        printf("Entry #%d: %s\n",i,dirlist[i]);
   
    return 0;
}
 in windows... while compiling it retuns errors!!!
what is #include <dirent.h>?????
niriaAsked:
Who is Participating?
 
van_dyConnect With a Mentor Commented:
i dont think this code will work on windows.
you should consder reding from :
http://www.c-for-dummies.com/lessons/chapter.15/01/
and coding around a bit
then u can post your code for further consideration by people here
0
 
van_dyCommented:
and yes, you dont need to keep creating a new question every time. just use
one single thread.
0
All Courses

From novice to tech pro — start learning today.