Link to home
Start Free TrialLog in
Avatar of niria
niria

asked on

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>?????
ASKER CERTIFIED SOLUTION
Avatar of van_dy
van_dy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of van_dy
van_dy

and yes, you dont need to keep creating a new question every time. just use
one single thread.