Public paste
sort problem
By: microhaxo | Date: Feb 24 2010 00:21 | Format: None | Expires: never | Size: 1.41 KB | Hits: 934

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sysexits.h>
  5.  
  6. #define MAX_NAMES 10000
  7. #define MAX_NAME_LEN 40
  8. void writefile(char c[100]);
  9. void sortstrings(void *array, size_t s);
  10. int compare(const void *a, const void *b);
  11. int main(int argc, char *argv[] )
  12. {
  13.         char c[100];
  14.         FILE *pFile;
  15.         long lFileLen;
  16.         int n;
  17.         size_t str_length =0;
  18.         if(argc != 3) // we need 3 things passed in, if not then it wont work
  19.         {
  20.                 printf("usage: %s readfile writefilen",argv[0]);
  21.         }
  22.         else
  23.         {
  24.                 printf("we made it this far");
  25.                
  26.                 // assume arg[1] is a file
  27.                 FILE *dataout;
  28.                 dataout = fopen(argv[2], "a+" );
  29.                 pFile = fopen(argv[1], "rw" );
  30.                 if(pFile == NULL)
  31.                 {
  32.                         printf("couldnt open the filen");
  33.                         printf("%sn",argv[1]);
  34.                         return 1;
  35.                 }
  36.                 else
  37.                 {
  38.                        
  39.                         // lets get the data stored..
  40.                         n = fread(c, 1, 50, pFile);
  41.                         c[n] = '';
  42.                         str_length = strlen(c);
  43.                         printf("size of strings %i",str_length);                       
  44.                         sortstrings(c, str_length);
  45.                        
  46.                 }              
  47.                 if(dataout == NULL)
  48.                 {
  49.                         printf("couldnt open the filen");
  50.                         printf("%sn",argv[2]);
  51.                         return 1;
  52.                 }
  53.                 else
  54.                 {
  55.                                        
  56.                         fwrite(c, str_length, 1, dataout);
  57.                         fclose(dataout);
  58.                         fclose(pFile);                 
  59.                        
  60.                         return(0);
  61.                        
  62.                 }
  63.        
  64.         }
  65.                
  66. }
  67. int compare(const void *a, const void *b)
  68. {
  69.         return strcmp(*(char **)a, *(char **)b);
  70. }
  71. void sortstrings(void *array, size_t s)
  72. {
  73.         qsort(array, s, sizeof(char *), compare);
  74. }