C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-08-2009, 05:42 PM   #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).
Malachi is offline   Reply With Quote
Old 02-08-2009, 05:43 PM   #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;
}
Malachi is offline   Reply With Quote
Old 02-08-2009, 06:57 PM   #3
Registered User
 
Join Date: Sep 2006
Posts: 2,511
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.
Adak is offline   Reply With Quote
Old 02-08-2009, 07:06 PM   #4
Registered User
 
Join Date: Feb 2009
Posts: 10
yeah, I dont know what a cmp function is... or how to use it.
Malachi is offline   Reply With Quote
Old 02-08-2009, 07:07 PM   #5
Registered User
 
Join Date: Feb 2009
Posts: 10
i put it in on the qsort line.
Malachi is offline   Reply With Quote
Old 02-08-2009, 07:19 PM   #6
Registered User
 
Join Date: Sep 2006
Posts: 2,511
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.
Adak is offline   Reply With Quote
Old 02-09-2009, 07:13 PM   #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
Malachi is offline   Reply With Quote
Old 02-09-2009, 07:14 PM   #8
Registered User
 
Join Date: Feb 2009
Posts: 10
I'm not sure what the components in the qsort function actually are.
Malachi is offline   Reply With Quote
Old 02-09-2009, 07:22 PM   #9
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 02-09-2009, 07:41 PM   #10
Registered User
 
Join Date: Sep 2006
Posts: 2,511
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.
Adak is offline   Reply With Quote
Old 02-09-2009, 07:52 PM   #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?
Malachi is offline   Reply With Quote
Old 02-09-2009, 07:56 PM   #12
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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);
tabstop is offline   Reply With Quote
Old 02-09-2009, 08:06 PM   #13
Registered User
 
Join Date: Sep 2006
Posts: 2,511
What compiler are you using, Malachi?

Did you try the simpler:
return(strcmp(a, b)); //?
Adak is offline   Reply With Quote
Old 02-09-2009, 08:06 PM   #14
Registered User
 
Join Date: Feb 2009
Posts: 10
tried that, the program runs but hits an error on the qsort line...
Malachi is offline   Reply With Quote
Old 02-09-2009, 08:07 PM   #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
Malachi is offline   Reply With Quote
Reply

Tags
alphabetical, file, input

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
File Writing Problem polskash C Programming 3 02-13-2009 10:47 AM
Formatting a text file... dagorsul C Programming 12 05-02-2008 03:53 AM
Dikumud maxorator C++ Programming 1 10-01-2005 06:39 AM
Batch file programming year2038bug Tech Board 10 09-05-2005 03:30 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C Programming 3 03-04-2005 02:46 PM


All times are GMT -6. The time now is 04:37 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22