EnricoilP
18-06-2010, 05:13
Salve a tutti,
Dovrei scrivere un programma in C che stampi i file della directory corrente in ordine alfabetico, usando opendir, readdir, closedir e qsort.
Il problema principale e' gestire un numero imprecisato di elementi. Al riempimento dell'ultimo elemento in un vettore, chiamo la realloc e raddoppio la dimensione.
Il problema e' che, in addtoarray, dopo aver salvato 6 elementi e aver chiamato la alloc 3 volte, non sa piu' continuare.
Mi sapreste dare una mano? Grazie mille. :)
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int addtoarray(char *myentry, char **listofentries, int *num_elements, int *num_allocated);
static int cmpstringp(const void *p1, const void *p2);
int addtoarray(char *myentry, char **listofentries, int *num_elements, int *num_allocated){
void ** tmplist;
if ((*num_elements) == (*num_allocated)){
if ((*num_allocated) == 0)
(* num_allocated) = 2;
else
(* num_allocated) *= 2;
if ( (tmplist = realloc(listofentries,((*num_allocated) * (sizeof(char *))))) == NULL){
perror("realloc error: not enough memory");
return -1;
}
listofentries = (char **)tmplist;
}
listofentries[*num_elements] = myentry;
printf("%s\n", listofentries[*num_elements]);
(*num_elements)++;
return 0;
}
static int cmpstringp(const void *p1, const void *p2){
return strcmp(* (char * const *) p1, * (char * const *) p2);
}
int main(){
printf("size of dirent is %d\n", sizeof(struct dirent));
char currentdir[2] = ".";
char **listofentries = (char**) malloc(1 * sizeof(char *));
int num_elements = 0;
int num_allocated = 1;
DIR * mystream;
struct dirent *myentry;
char * currentname;
int j;
if ((mystream = opendir((char *)¤tdir)) < 0)
perror("opendir error: couldn't open directory");
while ( (myentry = readdir(mystream)) != NULL ){
if ((addtoarray(myentry->d_name, listofentries, &num_elements, &num_allocated))<0){
perror("not enough memory available \n");
exit(0);
}
}
qsort(*listofentries, num_elements, sizeof(listofentries[0]), cmpstringp);
for (j = 0; j < num_elements; j++)
printf("%s\n",listofentries[j]);
if (closedir(mystream)<0)
perror("unable to close directory");
exit(0);
}
Dovrei scrivere un programma in C che stampi i file della directory corrente in ordine alfabetico, usando opendir, readdir, closedir e qsort.
Il problema principale e' gestire un numero imprecisato di elementi. Al riempimento dell'ultimo elemento in un vettore, chiamo la realloc e raddoppio la dimensione.
Il problema e' che, in addtoarray, dopo aver salvato 6 elementi e aver chiamato la alloc 3 volte, non sa piu' continuare.
Mi sapreste dare una mano? Grazie mille. :)
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int addtoarray(char *myentry, char **listofentries, int *num_elements, int *num_allocated);
static int cmpstringp(const void *p1, const void *p2);
int addtoarray(char *myentry, char **listofentries, int *num_elements, int *num_allocated){
void ** tmplist;
if ((*num_elements) == (*num_allocated)){
if ((*num_allocated) == 0)
(* num_allocated) = 2;
else
(* num_allocated) *= 2;
if ( (tmplist = realloc(listofentries,((*num_allocated) * (sizeof(char *))))) == NULL){
perror("realloc error: not enough memory");
return -1;
}
listofentries = (char **)tmplist;
}
listofentries[*num_elements] = myentry;
printf("%s\n", listofentries[*num_elements]);
(*num_elements)++;
return 0;
}
static int cmpstringp(const void *p1, const void *p2){
return strcmp(* (char * const *) p1, * (char * const *) p2);
}
int main(){
printf("size of dirent is %d\n", sizeof(struct dirent));
char currentdir[2] = ".";
char **listofentries = (char**) malloc(1 * sizeof(char *));
int num_elements = 0;
int num_allocated = 1;
DIR * mystream;
struct dirent *myentry;
char * currentname;
int j;
if ((mystream = opendir((char *)¤tdir)) < 0)
perror("opendir error: couldn't open directory");
while ( (myentry = readdir(mystream)) != NULL ){
if ((addtoarray(myentry->d_name, listofentries, &num_elements, &num_allocated))<0){
perror("not enough memory available \n");
exit(0);
}
}
qsort(*listofentries, num_elements, sizeof(listofentries[0]), cmpstringp);
for (j = 0; j < num_elements; j++)
printf("%s\n",listofentries[j]);
if (closedir(mystream)<0)
perror("unable to close directory");
exit(0);
}