Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

put file names in array

Posted on 2004-10-27
4
Medium Priority
?
207 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
Comment
Question by:niria
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
van_dy earned 120 total points
ID: 12424519
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
ID: 12424531
and yes, you dont need to keep creating a new question every time. just use
one single thread.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
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.

610 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