Solved

sorting and listing directory files

Posted on 2003-10-21
2
218 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers 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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now