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*size of(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],d entry->d_n ame);
cur_size++;
if(cur_size>=max_size){
max_size *= 2;
dirlist=(char**)realloc(di rlist,max_ size*sizeo f(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>?????
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
struct dirent* dentry;
size_t namelen;
int i;
if(!(d=opendir("."))){
fprintf(stderr,"Cannot open dir '.'");
exit(0);
}
while((dentry=readdir(d))!
namelen = strlen(dentry->d_name);
if(strcmp(&dentry->d_name[
continue; /* Skip entry */
dirlist[cur_size] = (char*) malloc(namelen+1);
strcpy(dirlist[cur_size],d
cur_size++;
if(cur_size>=max_size){
max_size *= 2;
dirlist=(char**)realloc(di
}
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
one single thread.