TIP: Click on subject to list as thread! ANSI
echo: locsysop
to: Paul Edwards
from: Bob Lawrence
date: 1996-11-22 08:09:40
subject: Bloody C

Hi Paul,
         I'm out of my depth and I can't make bloody C work.

  I want to read a line out of FILES, put it in a pointer array, sort
the line using the first 4 characters, and then write the sorted array
to the new file.

  This is the qsort() function... strcmp() straight out of the book.

int sort_function(const void *a, const void *b)
{
   return(strcmp((char *)a,(char *)b));
}

  And this is what I use to read the file.

int postsort(void)
{

   FILE     *files, *bak;
   int      i, n;
   char     line[256], *list[512];

   if ((bak = fopen("files.bak", "rb")) == NULL) return(1);
   post = fopen("files.20", "wb");
   n = 0;
   /*************  copy lines into array  *********/
   while (!feof(bak)) {
      fgets(line, 128, bak);
      if (line[0] > 32) {                           //ignore blanks
         list[n] = malloc(strlen(line) + 1);
         strcpy(list[n], line);
         n++;
      }
   }

  qsort(list, n, sizeof(list[0].nme) , sort_function);

   for(i = 0; i < n; i++) {
      printf("%s", list[i]);
      fprintf(post, "%s\n", list[i]);
      free(list[i]);
   }
   fclose(post); fclose(bak);
   return(0);
}

  My problem is with qsort(). It gets totally stuffed up with the long
and variable-length lines. How the bloody hell can I make it sort
on just the first few characters in each line? When I try to rewrite
sort_function() it won't let me truncate the strings because it's
passing void pointers!

Regards,
Bob

___ Blue Wave/QWK v2.12
@EOT:

---
* Origin: Precision Nonsense, Sydney (3:711/934.12)
SEEN-BY: 711/934 712/610
@PATH: 711/934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.