Solved

sorting and listing directory files

Posted on 2003-10-21
2
221 Views
Last Modified: 2010-04-15
directory files are being listed but not in order.  
code;

#include<stdio.h>
#include<stdlib.h>
#include<dir.h>
#include<conio.h>

#define      NORMAL            0x00
#define      READONLY            0x01
#define      HIDDEN            0x02
#define      SYSTEM            0x04
#define      VOLLABEL            0x08
#define      DIR            0x10
#define      ARCHIVE            0x20

int comp_Names(const void *, const void *);


main()
{

  struct ffblk fblock;
  char dirname[20];
  int i;

  puts("\nEnter directory name ?\n\n");
  gets(dirname);
  if(findfirst(dirname, &fblock, NORMAL) != 0)  {
      puts("\nERROR, FILE NOT FOUND \n\n");
      return 1;
  }
  qsort(dirname, 10, sizeof(char), comp_Names);    // 10 files are in the directory
  for(i = 1; i <= 10; i++)  {
     printf("\n%30s%10ld%10d%10d\n", fblock.ff_name,fblock.ff_fsize,fblock.ff_fdate,fblock.ff_ftime);
          findnext(&fblock);
  }
  puts("\nEND \n\n");
  getch();
  return 0;

}

int comp_Names(const void *aa, const void *bb)
{

      char *F, *G;
      int ee;

      F = (char *)aa;
      G = (char *)bb;

      if(strcmp(F, G) >  0) ee = 1;
      if(strcmp(F, G) <  0) ee = -1;
      if(strcmp(F, G) == 0) ee = 0;
      return(ee);

}

thanks for helping.
-Y
0
Comment
Question by:yunikon
2 Comments
 
LVL 8

Accepted Solution

by:
mnashadka earned 125 total points
ID: 9595782
There are a couple of issues with this code:
 if(findfirst(dirname, &fblock, NORMAL) != 0)  {
      puts("\nERROR, FILE NOT FOUND \n\n");
      return 1;
  }
  qsort(dirname, 10, sizeof(char), comp_Names);    // 10 files are in the directory
  for(i = 1; i <= 10; i++)  {
     printf("\n%30s%10ld%10d%10d\n", fblock.ff_name,fblock.ff_fsize,fblock.ff_fdate,fblock.ff_ftime);
         findnext(&fblock);
  }

First of all, you are doing a qsort after finding the first file and before finding the rest of the files.  This means that only the first file would really get sorted, but you need all of the files in the array before they get sorted.  Second, you're not ever copying the ff_name to an array (it would have to be two-dimensional).  Then, qsort is operating on the directory name, not the files.  And also, just as an efficiency thing, in comp_Names, you could do one strcmp, save that result into an int, and assign ee based on that int.  strcmp is relatively inefficient.  Good luck!!!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question