Solved

sorting and listing directory files

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
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…
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 opening and reading files in the C programming language.
Suggested Courses

622 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