Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9230705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:53:22+00:00 2026-06-18T05:53:22+00:00

I have a simple program that list files written in C. I’m just wondering

  • 0

I have a simple program that list files written in C. I’m just wondering what can I add to this code so the files print out in sorted order? Thanks

while ((directory = readdir(dir)) != NULL)
{
   printf("%s\n", directory->dir_name);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:53:23+00:00Added an answer on June 18, 2026 at 5:53 am

    You have first to store the names in an array, MAXDIRS is the max number of dirs (or you could use calloc to allocate dynamically the array)

    #include <string.h>
    #include <stdio.h> 
    #include <stdlib.h> 
    
    char dirs[MAXDIRS][512]; // you have to ensure correct allocation (eg names < 512 chars)
    int ndirs = 0; // number of dirs
    
    while ((directory = readdir(dir)) != NULL) {
       strcpy(dirs[ ndirs++ ], directory->d_name);
    }
    
    // Then sort the directories
    
    // need a sorting function
    int comp(void const *a, void const *b) { 
        char const *u = (char const *)a;
        char const *v = (char const *)b;
    
        return strcmp(u, v);
    }
    
    // use qsort to sort the dirs
    qsort (dirs, ndirs, sizeof(dirs[0]), comp);
    

    dirs now contains a sorted array of directories

    // display
    
    int i;
    
    for (i=0 ; i<ndirs ; i++) {
      printf("%s\n", dirs[i]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this simple program that computes salaries for four different worker types. It's
Well, i have written a simple python program that parses HTML with HTMLParser. Here
I have written a simple OpenGL program in C++ that displays a line joining
I have written a simple utility that loops through all C# files in my
I have a simple Python program that gets artwork from MP3 files. But when
I have made a program that list all files and folders(f&f) locating on sd
Say I have simple program that emulates a board game with a number of
I have a simple program that checks webpages for strings, example: Private Sub Button1_Click(ByVal
I have a simple program that creates a thread, loops twenty times and then
I have a simple program that reads data from a PNG into a 2D

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.