LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-03-2015, 01:45 AM   #1
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Rep: Reputation: 255Reputation: 255Reputation: 255
How to list and sort files in some directory by the names on Linux/Win in C ?


Hello,

How to list and sort files in some directory by the names on Linux/Win in C ?

The is a method using scandir, but I would be prefering to use readdir, DIR *mydirectory, ... !

Code:
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>

int
main(void)
{
   struct dirent **namelist;
   int n;

   n = scandir(".", &namelist, 0, alphasort);
   if (n < 0)
       perror("scandir");
   else {
       while (n--) {
       printf("%s\n", namelist[n]->d_name);
       free(namelist[n]);
       }
       free(namelist);
   }
}
 
Old 10-03-2015, 04:35 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
unix: opendir/readdir/closedir
Windows: FindFirstFile/FindNextFile/FindClose
sort: qsort
 
Old 10-03-2015, 05:45 AM   #3
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Original Poster
Rep: Reputation: 255Reputation: 255Reputation: 255
Quote:
Originally Posted by NevemTeve View Post
unix: opendir/readdir/closedir
Windows: FindFirstFile/FindNextFile/FindClose
sort: qsort
1) First, thank you for giving hints, however after long unsucessfull search, I would be please to get a simple example.

If you do not not, anyhow, thank you for giving some hints.

2) Secondly, I would advise to stick to the combination of "readdir" and "qsort", which is likely the most portable/compatible method for Win and Unix/BSD/*nix.

3) The real need of the user:
Would you have PLEASE a very simple basic minimal example (such as the one above)? please.



edit/ for your knowledge: "readdir" is fairly compatible under Windows as well.

Last edited by Xeratul; 10-03-2015 at 05:50 AM.
 
Old 10-03-2015, 09:22 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Last time we tried this you refused to post the code that allegedly produced the errors you reported. I see there hasn't been much progress since then.
 
Old 10-03-2015, 01:14 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
@OP: I've just google'd for 'readdir example' and I got 187000 results...
 
Old 10-03-2015, 04:09 PM   #6
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Original Poster
Rep: Reputation: 255Reputation: 255Reputation: 255
Quote:
Originally Posted by NevemTeve View Post
@OP: I've just google'd for 'readdir example' and I got 187000 results...
try readdir and qsort together.

Did you get any single example? - none.
 
Old 10-04-2015, 04:01 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Try to implement this algorithm:
Code:
opendir
while (readdir successfull) {
    store data
}
closedir
sort data with qsort
 
Old 10-06-2015, 12:56 AM   #8
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Original Poster
Rep: Reputation: 255Reputation: 255Reputation: 255
Quote:
Originally Posted by NevemTeve View Post
Try to implement this algorithm:
Code:
opendir
while (readdir successfull) {
    store data
}
closedir
sort data with qsort
Code:
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>

int
main(void)
{
   char idata[250][250]; //for simplicity in this example
   int n=0;

   dirp = opendir(".");
   while ( (dp = readdir(dirp)) != NULL) {
        strncpy(idata[n++],dp->d_name, 250);    
    }
    closedir(dirp);
   

  // ok here, please help !!
  // thank you. 
}

Last edited by Xeratul; 10-06-2015 at 01:22 AM.
 
Old 10-06-2015, 02:57 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Code:
/* xeratul.c */

#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static int compare_fun (const void *p, const void *q);

int main (void)
{
   char idata[250][250]; //for simplicity in this example
   unsigned n=0;
   DIR *dirp;
   struct dirent *dp;

   dirp = opendir(".");
   while ((dp = readdir(dirp)) != NULL && 
	   n < sizeof idata / sizeof idata[0]) {
        strncpy(idata[n++],dp->d_name, 250);    
    }
    closedir(dirp);
   
    if (n>1)
	qsort (idata, n, sizeof idata[0], compare_fun);
}

static int compare_fun (const void *p, const void *q)
{
    const char *l= p;
    const char *r= q;
    int cmp;

    cmp= strcmp (l, r);
    return cmp;
}
 
1 members found this post helpful.
Old 10-06-2015, 03:48 PM   #10
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Original Poster
Rep: Reputation: 255Reputation: 255Reputation: 255
Quote:
Originally Posted by NevemTeve View Post
!!
Sincerely Thank You !!

+1! +10!!!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: How to Output a List of Files to a File and Sort Them in Linux LXer Syndicated Linux News 0 07-05-2012 02:20 AM
[SOLVED] List files/directory names without directory contents? littlebigman Linux - Software 2 05-03-2011 04:42 AM
sort oldest 5 files in a directory tree recursively based on timestamp Linux abhelp Programming 1 06-04-2010 04:58 AM
vsftpd not list files when viewed from deifferent win machine win ftp application in anuj_sharma25 Linux - Newbie 2 11-22-2005 11:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration