Thread: Arranging a file into alphabetical order.

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    10

    Arranging a file into alphabetical order.

    Hey guys,
    just wondering if you can help me out with this program i've made. It's got a problem with the 4th component of the qsort function... not sure about the whole 'cmp (void)' thing. I just copied and pasted it from somewhere (i'm new to programming).

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    10

    here's my progress

    insert
    Code:
    #<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    char fname[128];
    char *fcontent[128];
    int i, j;
    int cmp(void *a, void *b);
    
    
    int main ( void )
    {
    printf("\nPlease input a filename for the input (including extension)");
    scanf("%s",fname);
    FILE *file = fopen ( fname, "rw" );
    	if ( file != NULL )
    	{
    	char line [ 128 ]; /* or other suitable maximum line size */
     
    		while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
    		{
    		fputs ( line, stdout ); /* write the line */
    		}
    		for (i = 0; i <100; i++)
            fcontent[i] = strdup(line);
            qsort(line, i, sizeof(line), cmp);
            for (j = 0; j < i; j++) puts(fcontent[j]);
    		return EXIT_SUCCESS;
    		}
    	else
    	{
    	perror ( fname ); /* why didn't the file open? */
    	}
    
    return 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Don't you need a real cmp function, for this to work?

    I don't see one.

    You've got the definition of the cmp function, but no function.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    10
    yeah, I dont know what a cmp function is... or how to use it.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    10
    i put it in on the qsort line.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have defined a cmp function, and on the qsort line, you have named the compare function.

    But you have not put in a cmp function!

    I'm giving you a bit of a rough handling because I object to people taking code from here, there, wherever, on the internet, and not having done the studying for it.

    As soon as I get my jaw to quit grinding my teeth, I'll post up something helpful.

    Code:
    /* qsort help */
    
       Sorts using the quicksort algorithm.
    
     Syntax:
       void qsort(void *base, size_t nelem, size_t width, int(*fcmp)
      (const void *, const void *));
    
     Prototype in:
     stdlib.h
    
     Remarks:
    qsort is an implementation of the "median of three" variant of the quicksort
    algorithm.
    
    qsort sorts the entries in a table by repeatedly calling the user-defined
    comparison function pointed to by fcmp.
    
     þ base points to the base (0th element) of
       the table to be sorted.
     þ nelem is the number of entries in the
       table.
     þ width is the size of each entry in the
       table, in bytes.
    
    *fcmp, the comparison function, accepts two arguments, elem1 and elem2, each
    a pointer to an entry in the table.
    
    The comparison function compares each of the pointed-to items (*elem1 and
    *elem2), and returns an integer based on the result of the comparison.
    
       *elem1  < *elem2 fcmp returns an integer < 0
       *elem1 == *elem2 fcmp returns 0
       *elem1  > *elem2 fcmp returns an integer > 0
    
    In the comparison, the less-than symbol (<) means the left element should
    appear before the right element in the final, sorted sequence.
    
    Similarly, the greater-than symbol (>) means the left element should appear
    after the right element in the final, sorted sequence.
    
     Return Value: None.
    
     Portability:
    qsort is available on UNIX systems and is defined in ANSI C.
    
     See Also:
      bsearch    lsearch
    
     Example:
     #include <stdio.h>
     #include <stdlib.h>
     #include <string.h>
    
     int sort_function( const void *a, const void *b);
    
     char list[5][4] = { "cat", "car", "cab", "cap", "can" };
    
     int main(void)
     {
        int  x;
    
        qsort((void *)list, 5, sizeof(list[0]), sort_function);
        for (x = 0; x < 5; x++)
           printf("%s\n", list[x]);
        return 0;
     }
    
     int sort_function( const void *a, const void *b) //this is the actual compare function!
     {
        return( strcmp(a,b) );
     }
    
    Last edited by Adak; 02-08-2009 at 07:27 PM.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    10
    Thanks,
    appreciate the gesture but to me, (who has about 15 hours experience with C) this doesn't make a whole lotta sense.
    Can you explain what that big block of code actually means pls

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    10
    I'm not sure what the components in the qsort function actually are.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That "big block of code" is the manual entry for qsort, cut and pasted into (probably) the wrong box. (In that it's for you to read, not to go into your program.) As you may have noticed, it is not code, it is English, so you should just read it.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That is the help file for qsort - it's not code to put into your program!

    It just explains what qsort is, in the C standard library, and how to use it, and then has a small simple example program, which you can paste into an empty project or c file, and run.

    The reason I posted it, and the reason I colored some of it in red, was because your program needs the part in red, and doesn't have it yet.

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    10
    Ok I know I need the actual function in there.
    Both when I try and put it in and when I run the program u posted it comes up with the following error:

    C:\PROGRAM FILES\SILVERFROST\PROGRAMMING WORK\CMPDEMO.C(21) : error 139 - Argument no 1 of 'strcmp' must be of type '<ptr>const char', not '<ptr>const void'
    *** Compilation failed

    Any idea what that means?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's strange, as you should not be forced to convert from void* to char* by any real compiler. If it will shut your compiler up, you can do
    Code:
    return strcmp((const char*)a, (const char*)b);

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What compiler are you using, Malachi?

    Did you try the simpler:
    return(strcmp(a, b)); //?

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    10
    tried that, the program runs but hits an error on the qsort line...

  15. #15
    Registered User
    Join Date
    Feb 2009
    Posts
    10
    that was to tabstop.

    I'm using FTN95 silverfrost.
    i'll try the simpler one now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM

Tags for this Thread