![]() |
| | #1 |
| Registered User Join Date: Feb 2009
Posts: 10
| Arranging a file into alphabetical order. 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 | |
| | #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 | |
| | #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 | |
| | #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 | |
| | #5 |
| Registered User Join Date: Feb 2009
Posts: 10
| i put it in on the qsort line. |
| Malachi is offline | |
| | #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 | |
| | #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 | |
| | #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 | |
| | #9 |
| and the Hat of Guessing 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 | |
| | #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 | |
| | #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 | |
| | #12 |
| and the Hat of Guessing 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 | |
| | #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 | |
| | #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 | |
| | #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 | |
![]() |
| Tags |
| alphabetical, file, input |
| Thread Tools | |
| Display Modes | |
|
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 |