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

  • SEARCH
  • Home
  • 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 8426309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:30:38+00:00 2026-06-10T04:30:38+00:00

I have to simulate the ls function of unix in C . I have

  • 0

I have to simulate the ls function of unix in C.

I have to create a program to “Get all files under any directory by type in C”.

I googled and found programs which get lists of files, but they are alphabetically sorted; I want sorted by type of file. Please, can anyone help me?

  • 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-10T04:30:40+00:00Added an answer on June 10, 2026 at 4:30 am

    You must store the files somewhere in memory. Since this looks like a school project, I’d suggest to load the file names into a linked list, and employ one of the algorithms for sorting linked lists. This might well be the purpose of the exercise itself.

    For file type let’s assume “extension”, i.e., a .MP3 is type “Fraunhofer MPEG Layer 3” even if somebody might have renamed a .WMA file and called it .MP3. To detect “true” file type you’d need to employ something called a “magic file”, and there is a libmagic out there, but is it worth it? (If it is a doctorate thesis or a commercial program the answer is ‘hell yes’. If the program is to be graded by your average professor, then you judge whether to risk being considered ‘too clever’).

    The linked list entry ought to be a struct containing the file name and a pointer to its extension; this last you can find by considering that the extension is “whatever follows the last dot in the file name”, so you can leverage the strrchr function. Remember that some files will have no extension.

    You judge whether to employ a memory-saving hack such as storing the pointer to the extension, or duplicating the extension with strdup. The former is faster and leaner, but you must remember that the first struct pointer (filename) MUST be freed, and the second ABSOLUTELY MUSTN’T. Having two pointers behave differently might be considered bad coding practice (it is) or a clever hack (it is), depending on whether you value maintenance time or speed/memory.

    As for the retrieval of file names itself, there’s no all-portable way, so this has led to the development of libraries such as Boost. But for a school project, maybe you could restrict to POSIX systems and use opendir, readdir, closedir and stat.

    A good practice and a wise thing to do would be to separate the operations (directory retrieval, list sorting, list display) in different functions, so that you can test them separately and incrementally (e.g.: the folder retrieval does retrieve everything and you can display the files in unsorted order, etc.).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code in Python/PyMongo like ( code should simulate inner join and all
I am trying to write a program to simulate a card game. I have
I have a simple function written in Oracle 9i (version 9.2.0.4.0) to simulate an
I have a function that uses its own params but also checks if any
my iphone can't use jquery , so i have to simulate $ function by
I currently have this: function submit() { document.getElementById(lostpasswordform).click(); // Simulates button click document.lostpasswordform.submit(); //
I have to simulate a USB Device for automation and testing purposes (in Linux).
I have to simulate family tree in prolog. And i have problem of symetrical
I have to simulate a game where each player has turns and needs to
I'm debugging a network application. I have to simulate some of the data exchanged

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.