Prefix tree implementation for C - c

I am trying to write a C program that provides autocomplete suggestions looking at the contents of a directory. I have most of it done, however I get errors where directory names contain symbols that are not alphabets (such as . , / _). I get errors whenever I try to change things, including array size and looping values. Could you please take a look at my code and suggest what changes I need to make to include symbols in autocomplete suggestions.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#define MAX 1000
struct node {
int data;
struct node *array[26];
};
struct node* new_node(struct node *h)
{
int i;
h = malloc(sizeof (struct node));
for (i = 0; i < 24; ++i)
h->array[i] = 0;
return h;
}
struct node* insert(struct node *h, char *c, int value)
{
int i;
if (strlen(c) == 0)
return h;
if (h == NULL)
h = new_node(h);
struct node *p = h;
for (i = 0; i < strlen(c); ++i) {
if (p->array[c[i] - 'a'] == NULL)
p->array[c[i] - 'a'] = malloc(sizeof (struct node));
p = p->array[c[i] - 'a'];
}
p->data = value;
return h;
}
int dfs(struct node *h, char *dat)
{
int i;
char k[1000] = {0};
char a[2] = {0};
if (h == NULL)
return 0;
if (h->data > 0)
printf("Match: %s\n", dat);
for(i = 0; i < 26; ++i) {
strcpy(k, dat);
a[0] = i + 'a';
a[1] = '\0';
strcat(k, a);
dfs(h->array[i], k);
}
}
void search(struct node *h, char *s, char *dat)
{
int i;
char l[1000] = {0};
char a[2];
strcpy(l, dat);
if (strlen(s) > 0) {
a[0] = s[0];
a[1] = '\0';
if(h->array[a[0]-'a'] != NULL) {
strcat(dat, a);
search(h->array[a[0]-'a'], s+1, dat);
} else
printf("No Match \n");
} else {
if (h->data != 0)
printf("Match: ");
for (i = 0; i < 26; ++i) {
strcpy(l, dat);
a[0] = i + 'a';
a[1] = '\0';
strcat(l, a);
dfs(h->array[i], l);
}
}
}
struct node* read_keys(struct node *h, char *file)
{
char s[MAX];
FILE *a = fopen(file, "r");
if (a == NULL)
printf("Error while opening file");
else
while (feof(a) == 0) {
fscanf(a, "%s", s);
h = insert(h, s, 1);
}
return h;
}
int main()
{
DIR *d;
struct dirent *dir;
char direct[MAX];
printf("Enter a folder name: ");
scanf("%s",direct);
d = opendir(direct);
FILE *f = fopen("file.txt", "w");
if (f == NULL)
{
printf("Error opening file!\n");
exit(1);
}
if (d)
{
while ((dir = readdir(d)) != NULL)
{
fprintf(f,"%s\n", dir->d_name);
}
}
fclose(f);
char c[1000];
FILE *fptr;
if ((fptr = fopen("file.txt", "r")) == NULL)
{
printf("Error! opening file");
exit(1);
}
FILE* file = fopen("file.txt", "r");
char line[1000];
while (fgets(line, sizeof(line), file)) {
printf("%s", line);
}
fclose(fptr);
printf("Enter beginning of filename \n");
struct node *h = 0;
h = read_keys(h, "file.txt");
char s[MAX];
scanf("%s", s);
char dat[1000] = "";
search(h, s, dat);
return 0;
}

Related

Storing strings to Linked list in C

In my problem, I need to read lines from a txt file.
Each line contains student_id, name, birthdate, gender, department, and grade(class).
e.g ( 155898933;Nuh Kaplan;26/01/1998;M;ME;2 )
Then I need to store them in a Linked List.
I declared my structure as follows:
struct Node{
int id;
char name[100];
int date; //YYYYMMDD (String date will be converted to integer. I created dateStrInt function for this conversion.)
char gender[2];
char departmentCode[6];
int classGrade;
struct Node *next;
};
Then I read lines and store in an character array with this part of code:
if (fptr != NULL) {
while (fgets (buff, 100, fptr)) {
char* split = strtok(buff, ";");
while( split != NULL ) {
strcpy(arr[i][j], split);
j++;
split = strtok(NULL, ";");
}
i++;
j = 0;
}
}
After I stored lines into character array, I need to store them in a linked list. I tried to do it as follows:
for(int i = 0; i < len; i++) {
p = (struct Node*) malloc(sizeof(struct Node));
j = 0;
p->id = (int)arr[i][j];
strcpy(p->name, arr[i][j+1]);
p->date = dateStrInt(arr[i][j+2]);
strcpy(p->gender,arr[i][j+3]);
strcpy(p->departmentCode, arr[i][j+4]);
p->classGrade = (int)arr[i][j+5];
j = 0;
if(list == NULL){
list = p;
p -> next = NULL;
last = p;
}
else {
last -> next = p;
p -> next = NULL;
last = p;
}
}
However when I run my code, I get garbage values for student id and classGrade.
Can you help me to solve this problem?
My hole code:
#include <stdio.h>
#include<string.h>
#include <stdlib.h>
#include <ctype.h>
struct Node {
int id;
char name[100];
int date; //YYYYMMDD
char gender[2];
char departmentCode[6];
int classGrade;
struct Node *next;
};
int dateStrInt(char str[]) {
int d, m, y;
sscanf(str, "%d/%d/%d", &d, &m, &y);
return (y * 10000 + m * 100 + d);
}
int countLines(char* filename) {
int count = 0;
FILE *fptr;
fptr = fopen(filename, "r");
if(fptr == NULL){
printf("File does not exist.");
exit(1);
}
else {
for(char ch = getc(fptr); ch != EOF; ch = getc(fptr)) {
if(ch == '\n'){
count++;
}
}
}
return count + 1;
}
struct Node* createList(struct Node* list, int len, char filename[]){
struct Node* last;
struct Node* p;
char buff[100];
char arr[len][6][100];
int i = 0, j = 0;
FILE* fptr = fopen(filename,"r");
if (fptr != NULL) {
while (fgets (buff, 100, fptr)) {
char* split = strtok(buff, ";");
while( split != NULL ) {
strcpy(arr[i][j], split);
j++;
split = strtok(NULL, ";");
}
i++;
j = 0;
}
}
int o = 0;
for(int k = 0; k < len; k++){
printf("\nID = %s", arr[k][o]);
printf("\nName = %s", arr[k][o+1]);
printf("\nBirth Date = %s", arr[k][o+2]);
printf("\nGender = %s", arr[k][o+3]);
printf("\nDepartment = %s", arr[k][o+4]);
printf("\nGrade = %s", arr[k][o+5]);
o = 0;
}
for(int i = 0; i < len; i++){
p = (struct Node*) malloc(sizeof(struct Node));
j = 0;
p->id = (int)arr[i][j];
strcpy(p->name, arr[i][j+1]);
p->date = dateStrInt(arr[i][j+2]);
strcpy(p->gender,arr[i][j+3]);
strcpy(p->departmentCode, arr[i][j+4]);
p->classGrade = (int)arr[i][j+5];
j = 0;
if(list == NULL){
list = p;
p -> next = NULL;
last = p;
}
else {
last -> next = p;
p -> next = NULL;
last = p;
}
}
return list;
}
void printList(struct Node *list, int len){
struct Node *p;
p = list;
int cnt = 0;
printf("\nThe list = ");
while(p != NULL) {
if(cnt != len - 1) {
printf("\n{%d, %s, %d, %s, %s, %d} ---->",p->id, p->name, p->date,
p->gender, p->departmentCode, p->classGrade);
cnt++;
}
else
printf("\n{%d, %s, %d, %s, %s, %d} ",p->id, p->name, p->date,
p->gender, p->departmentCode, p->classGrade);
p = p->next;
}
}
int main(void) {
struct Node *head = NULL;
char filename[10];
printf("Enter file name: ");
scanf("%s", filename);
int len = countLines(filename);
struct Node* list = createList(head, len, filename);
printList(list, len);
}
test.txt file :
149875280;Burcu Aksu;04/04/1994;F;CS;3
180201201;Mustafa Kursat Yavuz Tur;12/06/1996;M;CS;3
Output:
Enter file name: test.txt
ID = 149875280
Name = Burcu Aksu
Birth Date = 04/04/1994
Gender = F
Department = CS
Grade = 3
ID = 180201201
Name = Mustafa Kursat Yavuz Tur
Birth Date = 12/06/1996
Gender = M
Department = CS
Grade = 3
The list =
{-1883589280, Burcu Aksu, 19940404, F, CS, -1883588780} ---->
{-1883588680, Mustafa Kursat Yavuz Tur, 19960612, M, CS, -1883588180}
Why are you declaring char array, instead you should be creating array of Node type. Just typecasting the string value to int will not convert the 2 integer fields correctly. Use the conversion function like atoi().
Change this line,
p->id = (int)arr[i][j];
to
p->id = atoi(arr[i][j]);

Read from file to linked list C

I'm new to the C language, and I'm trying to make a calendar using SDL2, but for some reason my code just skips storing the first data. When I try to return with the "head" of my linked list (which stores structures), some of the data is saved, but some is not. I have a function called linecutter. It should work fine according to my tests. The structure of the file is something like
for some reason the comment part returns correctly. If I printf the location data, it returns correctly, but otherwise it doesn't work as intended.
An examle of a file used:
Prog spec;2020/10/05;2020/11/01;00/00;23/59;Infoc;shouldnt forget it and leave to the last min
something;2020/10/05;2020/11/01;00/00;23/59;location;just a comment
asd asd;2020/2/02;2020/9/01;00/00;23/59;Infoc;lkasjd laksdj
This code should print the event names twice. Once when reading the file, once when checking in the main. But it only prints when reading.
#include <stdio.h>
#include <stdlib.h>
typedef struct Date{
int y;
int m;
int d;
} Date;
typedef struct Time{
int h;
int m;
} Time;
typedef struct Event{
char* name;
Date startdate;
Date enddate;
Time starttime;
Time endtime;
char* location;
char* comment;
} Event;
typedef struct Lista{
Event event;
struct Lista * next;
} Lista;
Date datecreate(char*todate)
{
Date *tmp = (Date *)malloc(sizeof(Date)*1);
int len = strlen(todate);
char date[len];
strcpy(date,todate);
char y[5];
char m[5];
char d[4];
int i =0;
for(; i<4; i++)
y[i] = date[i];
i++;
for(int var = 0; i<7; var++, i++)
{
m[var] = date[i];
}
i++;
for(int var = 0; i<10; var++, i++)
{
d[var] = date[i];
}
tmp->d = atoi(d);
tmp->m = atoi(m);
tmp->y = atoi(y);
return *tmp;
}
Time timecreator(char*totime)
{
Time tmp;
char h[3];
char m[3];
for(int i = 0; i<2; i++)
h[i] = totime[i];
for(int i = 4; i<6; i++)
m[i-4] = totime[i];
h[3] = '\0';
m[3] = '\0';
tmp.h = atoi(h);
tmp.m = atoi(m);
return tmp;
}
Event linecutter(char* line)
{
int i = -1;
int loc = 0;
char name[256], startdate[12],enddate[12],starttime[7],endtime[7],location[256],comment[512];
do{
i++;
name[i] = line[loc];
loc++;
}
while(name[i] !=';');
name[i] = '\0';
i=-1;
do{
i++;
startdate[i] = line[loc];
loc++;
}
while(startdate[i] !=';');
startdate[i] = '\0';
i=-1;
do{
i++;
enddate[i] = line[loc];
loc++;
}
while(enddate[i] !=';');
enddate[i] = '\0';
i=-1;
do{
i++;
starttime[i] = line[loc];
loc++;
}
while(starttime[i] !=';');
starttime[i] = '\0';
i=-1;
do{
i++;
endtime[i] = line[loc];
loc++;
}
while(endtime[i] !=';');
endtime[i] = '\0';
i=-1;
do{
i++;
location[i] = line[loc];
loc++;
}
while(location[i] !=';');
location[i] = '\0';
i=-1;
do{
i++;
comment[i] = line[loc];
loc++;
}
while(comment[i] !='\0');
comment[i] = '\0';
Event tmp;
tmp.name = name;
tmp.location = location;
tmp.comment = comment;
tmp.starttime = timecreator(starttime);
tmp.startdate = datecreate(startdate);
tmp.enddate = datecreate(enddate);
tmp.endtime = timecreator(endtime);
return tmp;
}
Lista * read(void)
{
FILE *fp;
char line[5000];
fp = fopen("esemenyek.txt" , "rw+");
if(fp == NULL) {
perror("error opening");
return NULL;
}
char buf[3];
fscanf(fp, "%3c", buf);
if (memcmp(buf, "\xEF\xBB\xBF", 3) != 0) //this is just for the encoding stuff
fseek(fp, 0, SEEK_SET);
/* ... handling the file ... */
Lista * head= NULL;
Lista * current = NULL;
while(fgets(line, sizeof(line), fp)!=NULL){
Lista *node = (Lista*)malloc(sizeof(Lista));
if(node == NULL)
{
perror("error");
return NULL;
}
node->event = linecutter(line);
node->next =NULL;
if(head== NULL){
current = head = node;
} else {
current = current->next= node;
}
for(int i = 0; i<500; i++)
line[i] ='\0';
printf("%s\n", current->event.name);
}
fclose(fp);
return head;
}
int main(void){
Lista * lst = read();
Lista *move;
for (move= lst; move!= NULL; move= move->next)
printf("%s\n", move->event.name);
return 0;
}
What am I doing wrong, or why is this not working? If someone could help me, I'd really appreciate it. I found another post here, it helped me to get here, but I don't know how to proceed from here.

Filling Gtk Tree Store from a file

I'm trying to fill a gtk tree store from a linked list but i get the segmentation fault (core dumped) problem here is my code File to struct is a linked list filled a struct to a professor and a next the professor is defined by a name matiere password pseudo...etc
store = gtk_list_store_new (NUM_COLUMNNS,G_TYPE_INT,G_TYPE_STRING,G_TYPE_STRING,G_TYPE_STRING,G_TYPE_STRING,G_TYPE_INT);
File_to_struct *p=head;
/* add data to the list store */
while(p!=NULL)
{
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter,
COLUMNN_ID,p->professeur.ID,
COLUMNN_NOM,p->professeur.nom,
COLUMNN_MATIERE,p->professeur.matiere,
COLUMNN_PSEUD,p->professeur.pseudo,
COLUMNN_PASS,p->professeur.password,
COLUMNN_VALIDE,p->professeur.valide,
-1);
p=p->suivant;
}
FILE fichier=fopen("professeur.txt","r");
Prof professeur;
File_to_struct *tete=(File_to_struct)malloc(sizeof(File_to_struct));
tete=NULL;
rewind(fichier);
while((!feof(fichier)))
{
fscanf(fichier,"\n%s %s %s %s %d %d\n",professeur.nom,professeur.matiere,professeur.pseudo,professeur.password, &pro‌​fesseur.valide,&professeur.ID);
tete=inserer(tete,professeur); }
typedef struct prof{
int ID;
int valide;
char nom[40];
char matiere[40];
char password[40];
char pseudo[40]; }Prof;
typedef struct file_to_struct{
Prof professeur;
struct file_to_struct *suivant; }File_to_struct;
You have problems when filling the list, this is an example of how to do it correctly
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct Prof
{
int ID;
int valide;
char nom[40];
char matiere[40];
char password[40];
char pseudo[40];
} Prof;
typedef struct File_to_struct
{
Prof professeur;
struct File_to_struct *suivant;
} File_to_struct;
File_to_struct *
inserer(File_to_struct *Debut, Prof profs)
{
File_to_struct *nv;
File_to_struct *actuel;
nv = malloc(sizeof(File_to_struct));
if (nv == NULL)
return Debut;
nv->suivant = NULL;
nv->professeur = profs;
actuel = Debut;
if (Debut == NULL)
Debut = nv;
else
{
while (actuel->suivant != NULL)
actuel = actuel->suivant;
actuel->suivant = nv;
}
return Debut;
}
char **
splitString(const char *const text, const char *const delimiter, int *count)
{
char *copy;
char *pointer;
char *token;
char *saveptr;
char **list;
if ((text == NULL) || (count == NULL) || (delimiter == NULL))
return NULL;
copy = strdup(text);
*count = 0;
pointer = copy;
list = NULL;
while ((token = strtok_r(pointer, delimiter, &saveptr)) != NULL)
{
void *auxiliary;
auxiliary = realloc(list, (1 + *count) * sizeof(char *));
if (auxiliary == NULL)
{
while (*count >= 0)
{
free(list[*count]);
*count -= 1;
}
free(copy);
free(list);
return NULL;
}
list = auxiliary;
list[*count] = strdup(token);
*count += 1;
pointer = NULL;
}
free(copy);
return list;
}
Prof
extractProfesseur(const char *const line)
{
char **list;
int count;
Prof prof;
memset(&prof, 0, sizeof(prof));
list = splitString(line, " ", &count);
if (count < 6)
{
while (--count >= 0)
free(list[count]);
free(list);
return prof;
}
prof.ID = strtol(list[4], NULL, 10);
prof.valide = strtol(list[5], NULL, 10);
strncpy(prof.nom, list[0], 39);
strncpy(prof.matiere, list[1], 39);
strncpy(prof.pseudo, list[2], 39);
strncpy(prof.password, list[3], 39);
while (--count >= 0)
free(list[count]);
free(list);
return prof;
}
int
main()
{
FILE *fichier;
File_to_struct *tete;
File_to_struct *actuel;
char line[256];
fichier = fopen("professeur.txt","r");
if (fichier == NULL)
return -1; /* n'pouvez pas de ouvrir le fichier */
tete = NULL;
rewind(fichier);
while (fgets(line, sizeof(line), fichier) != NULL)
{
Prof professeur;
professeur = extractProfesseur(line);
tete = inserer(tete, professeur);
}
actuel = tete;
while (actuel != NULL)
{
Prof prof;
File_to_struct *previous;
prof = actuel->professeur;
previous = actuel;
actuel = actuel->suivant;
printf("%s %s %s %s %d %d\n", prof.nom, prof.matiere,
prof.pseudo, prof.password, prof.valide, prof.ID);
free(previous);
}
fclose(fichier);
return 0;
}

Segmentation fault while inserting into a hash table

I want to be able to use my getNextWord function to return a pointer to the next word in the file. I think I'm getting the seg fault while inserting but I just can't figure it out. Any help on this would be excellent. Also, I should probably find a better way of getting my hash_table_size than increasing a count for the total number of words in the file then rewinding. How can I make the size grow automatically?
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
int hash_table_size;
char* getNextWord(FILE* fd) {
char c;
char buffer[256];
int putChar = 0;
while((c = fgetc(fd)) != EOF) {
if(isalnum(c)) break;
}
if(c == EOF) return NULL;
buffer[putChar++] = c;
while((c = fgetc(fd)) != EOF) {
if(isspace(c) || putChar >= 256 -1) break;
if(isalnum(c))
buffer[putChar++] = c;
}
buffer[putChar] = '\0';
return strdup(buffer);
}
struct node {
struct node *next;
int count;
char* key;
};
struct list {
struct node *head;
int count;
};
struct list *hashTable = NULL;
/*
* djb2 hash function
*/
unsigned int hash(unsigned char *str) {
unsigned int hash = 5381;
int c;
while(c == *str++)
hash = ((hash << 5) + hash) + c;
return (hash % hash_table_size);
}
struct node* createNode(char *key) {
struct node *new_node;
new_node = (struct node *)malloc(sizeof(struct node));
strcpy(new_node->key, key);
new_node->next = NULL;
return new_node;
}
void hashInsert(char *str) {
int hash_dex = hash(str);
struct node *new_node = createNode(str);
if(!hashTable[hash_dex].head) {
hashTable[hash_dex].head = new_node;
hashTable[hash_dex].count = 1;
return;
}
new_node->next = (hashTable[hash_dex].head);
hashTable[hash_dex].head = new_node;
hashTable[hash_dex].count++;
return;
}
void display() {
struct node *current;
int i;
while(i < hash_table_size) {
if(hashTable[i].count == 0)
continue;
current = hashTable[i].head;
if(!current)
continue;
while(current != NULL) {
char tmp[256];
strcpy(tmp, current->key);
printf("%s", tmp);
current = current->next;
}
}
return;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: ./hashFile textfile\n");
}
else {
FILE *file = fopen(argv[1], "r");
if(file == 0) {
printf("Could not open file\n");
}
else {
char *new_word;
while((new_word = getNextWord(file)) != NULL) {
hash_table_size++;
}
rewind(file);
hashTable = (struct list *)calloc(hash_table_size, sizeof(struct list));
while((new_word = getNextWord(file)) != NULL) {
hashInsert(new_word);
}
display();
fclose(file);
}
}
return 0;
}
int c;
while(c == *str++)
hash = ((hash << 5) + hash) + c;
c is not initialized here. As is i in display function. Please enable all the compiler warnings and fix them.
Also:
char c;
char buffer[256];
int putChar = 0;
while((c = fgetc(fd)) != EOF) {
if(isalnum(c)) break;
}
c has to be of type int not char.
change
while(c == *str++)
to
while(c = *str++)

save file listing into array or something else C

I have this function
void file_listing(){
DIR *dp;
struct stat fileStat;
struct dirent *ep;
int file=0;
dp = opendir ("./");
if (dp != NULL){
while ((ep = readdir(dp))){
char *c = ep->d_name;
char d = *c; /* first char */
if((file=open(ep->d_name,O_RDONLY)) < -1){
perror("Errore apertura file");
}
if(fstat(file,&fileStat)){ /*file info */
perror("Errore funzione fstat");
}
if(S_ISDIR(fileStat.st_mode)){ /* directory NOT listed */
continue;
}
else{
if(d != '.'){ /* if filename DOESN'T start with . will be listed */
"save into someting" (ep->d_name);
}
else{
continue; /* altrimenti non lo listo */
}
}
}
(void) closedir (dp);
}
else{
perror ("Impossibile aprire la directory");
return 1;
}
}
I want to save into an array or a struct or a list or something else the result of file listing but i don't know how to do it.
Thanks in advance!
Here is a sample function that stores the file listing in array and returns the number of entries:
#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <malloc.h>
size_t file_list(const char *path, char ***ls) {
size_t count = 0;
size_t length = 0;
DIR *dp = NULL;
struct dirent *ep = NULL;
dp = opendir(path);
if(NULL == dp) {
fprintf(stderr, "no such directory: '%s'", path);
return 0;
}
*ls = NULL;
ep = readdir(dp);
while(NULL != ep){
count++;
ep = readdir(dp);
}
rewinddir(dp);
*ls = calloc(count, sizeof(char *));
count = 0;
ep = readdir(dp);
while(NULL != ep){
(*ls)[count++] = strdup(ep->d_name);
ep = readdir(dp);
}
closedir(dp);
return count;
}
int main(int argc, char **argv) {
char **files;
size_t count;
int i;
count = file_list("/home/rgerganov", &files);
for (i = 0; i < count; i++) {
printf("%s\n", files[i]);
}
}
Note that I iterate twice over the directory - first time to get the files count and second time to save the results. This won't work correctly if you add/remove files in the directory while this is being executed.
An other example, more cleaner but not thread safe because readdir() is not:
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
static void *realloc_or_free(void *ptr, size_t size) {
void *tmp = realloc(ptr, size);
if (tmp == NULL) {
free(ptr);
}
return tmp;
}
static int get_dirent_dir(char const *path, struct dirent **result,
size_t *size) {
DIR *dir = opendir(path);
if (dir == NULL) {
closedir(dir);
return -1;
}
struct dirent *array = NULL;
size_t i = 0;
size_t used = 0;
struct dirent *dirent;
while ((dirent = readdir(dir)) != NULL) {
if (used == i) {
i += 42; // why not?
array = realloc_or_free(array, sizeof *array * i);
if (array == NULL) {
closedir(dir);
return -1;
}
}
array[used++] = *dirent;
}
struct dirent *tmp = realloc(array, sizeof *array * used);
if (tmp != NULL) {
array = tmp;
}
*result = array;
*size = used;
closedir(dir);
return 0;
}
static int cmp_dirent_aux(struct dirent const *a, struct dirent const *b) {
return strcmp(a->d_name, b->d_name);
}
static int cmp_dirent(void const *a, void const *b) {
return cmp_dirent_aux(a, b);
}
int main(void) {
struct dirent *result;
size_t size;
if (get_dirent_dir(".", &result, &size) != 0) {
perror("get_file_dir()");
}
qsort(result, size, sizeof *result, &cmp_dirent);
for (size_t i = 0; i < size; i++) {
printf("%s\n", result[i].d_name);
}
free(result);
}

Resources