Solved

put file names in array

Posted on 2004-10-27
201 Views
Last Modified: 2010-04-15
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>?????
0
Question by:niria
    2 Comments
     
    LVL 5

    Accepted Solution

    by:
    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
     
    LVL 5

    Expert Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
    The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

    856 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now