Home > Enterprise >  how to display the filenames in ascending order according to their sizes of current directory using
how to display the filenames in ascending order according to their sizes of current directory using

Time:05-28

I am trying to sort file names according to their sizes using a system call in c.

I tried this...

#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <limits.h>
#include <string.h>

int main() {
    DIR *dir1, *dir2;
    int fd, cur_size, min_size;
    struct dirent *dirent1, *dirent2;
    struct stat st1, st2;
    char min_file_name[1000];
    char filename[1000];
    dir1 = opendir(".");
    while ((dirent1 = readdir(dir1)) != NULL) {
        min_size = INT_MAX;
        dir2 = opendir(".");
        while ((dirent2 = readdir(dir2)) != NULL) {
            stat(dirent2->d_name, &st1);
            cur_size = st1.st_size;
            strcpy(filename, dirent2->d_name);
            if (cur_size <= min_size && strcmp(min_file_name, filename) != 0) {
                min_size = cur_size;
                strcpy(min_file_name, dirent2->d_name);
            }
        }
        printf("File name = %s || size = %d \n", min_file_name, min_size);
    }
}

Output

amol@amol-Ideapad-320:~/AOS$ gcc q3.c
amol@amol-Ideapad-320:~/AOS$ ./a.out

File name =  || size = 0 
File name =  || size = 0 
File name =  || size = 0 
File name =  || size = 0 

CodePudding user response:

I'm afraid I don't understand the logic in your nested loop approach.

To produce a list in a given order, you can construct a sorted list of the entries in the directory and print that.

Here is a modified version using a simplistic quadratic insertion sort on a linked list:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

struct entry {
    char *name;
    long long int size;
    struct entry *next;
};

int main() {
    DIR *dir;
    struct dirent *dp;
    struct stat st;
    struct entry *head = NULL, *ep, **npp;

    dir = opendir(".");
    if (dir == NULL) {
        perror("cannot open directory");
        return 1;
    }
    while ((dp = readdir(dir)) != NULL) {
        ep = calloc(sizeof(*ep), 1);
        if (ep == NULL) {
            perror("cannot allocate memory");
            return 1;
        }
        ep->name = strdup(dp->d_name);
        if (ep->name == NULL) {
            perror("cannot allocate memory");
            return 1;
        }
        if (!stat(dp->d_name, &st)) {
            ep->size = st.st_size;
        }
        for (npp = &head; *npp && (*npp)->size <= ep->size; npp = &(*npp)->next)
            continue;
        ep->next = *npp;
        *npp = ep;
    }
    closedir(dir);
    for (ep = head; ep; ep = ep->next) {
        printf("%s\n", ep->name);
    }
    while ((ep = head) != NULL) {
        head = ep->next;
        free(ep->name);
        free(ep);
    }
    return 0;
}
  • Related