Solved

sorting and listing directory files

Posted on 2003-10-21
2
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 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 how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

732 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