Velocity Reviews > Problem by sorting array of structures using qsort

# Problem by sorting array of structures using qsort

zavnobih@gmail.com
Guest
Posts: n/a

 03-21-2007
Below is my code. Qsort is not working. What is wrong?

************************************************** **********************
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>

#define MAX 10

int compare( const void *arg1, const void *arg2 );

typedef struct
{
char s_line[20];
} lines_to_sort_t;

int main (int argc, char **argv)
{

int c;
FILE *fp;
char text_ln[20];
lines_to_sort_t table[MAX];

while ( (c = getopt(argc, argv, "r") ) != EOF )
{
switch (c)
{
case 'r':
printf("aaa \n");
break;
}
}

int i=0;
while (optind < argc)
{

fp = fopen(argv[optind], "r");
while ( fgets(text_ln, sizeof(text_ln), fp) != NULL)
{

strcpy(table[i].s_line, text_ln);
i++;
}

optind++;
}

qsort(table, sizeof table / sizeof table[0], sizeof table[0],
compare);

i = 0;
while (i < 3 )
{
printf( "%d: %s",i, table[i].s_line );
i++;
}
}

int compare( const void *arg1, const void *arg2 )
{

const lines_to_sort_t *p1 = arg1;
const lines_to_sort_t *p2 = arg2;
return strcmp(p1->s_line, p2->s_line);

}

Eric Sosman
Guest
Posts: n/a

 03-21-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Below is my code. Qsort is not working. What is wrong?
> [...]

Hard to tell, since you didn't describe the way in
which your code is "not working." What did you expect
it to do, what did it do differently, and in what way
is that behavior "not working?"

Still, one thing leaps out: You fill the first `i'
elements of table[], but then you sort all `MAX' of them.
MAX is 10, so if `i' is (for example) 7 there will be
three trailing elements of table[] that have never been
given any values. They "contain garbage" (or worse), and
when you try to sort them along with the 7 valid entries
there's no telling what might happen.

There may be other problems in your code, too -- but
it's loaded up with non-Standard headers and functions that
I don't have the patience to study. Fix the known problem
and see if things become more understandable -- and if they
don't, good luck!

--
Eric Sosman
(E-Mail Removed)lid

zavnobih@gmail.com
Guest
Posts: n/a

 03-21-2007
On 21 Mrz., 02:35, Eric Sosman <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > Below is my code. Qsort is not working. What is wrong?
> > [...]

>
> Hard to tell, since you didn't describe the way in
> which your code is "not working." What did you expect
> it to do, what did it do differently, and in what way
> is that behavior "not working?"
>
> Still, one thing leaps out: You fill the first `i'
> elements of table[], but then you sort all `MAX' of them.
> MAX is 10, so if `i' is (for example) 7 there will be
> three trailing elements of table[] that have never been
> given any values. They "contain garbage" (or worse), and
> when you try to sort them along with the 7 valid entries
> there's no telling what might happen.
>
> There may be other problems in your code, too -- but
> it's loaded up with non-Standard headers and functions that
> I don't have the patience to study. Fix the known problem
> and see if things become more understandable -- and if they
> don't, good luck!
>
> --
> Eric Sosman
> (E-Mail Removed)

Thank You Eric.
Problem was MAX number of elements. When I change MAX number to
correspond with number of added elements it works, but when this
number differs I have a problem.
How could I fix this?

user923005
Guest
Posts: n/a

 03-21-2007
/*
Some clues found below.
I made things a bit bigger just so I wouldn't have to worry about
the string size of items in files I had laying around.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

#define MAX 100

int compare(const void *arg1, const void *arg2);

typedef struct {
char s_line[80];
} lines_to_sort_t;

int main(int argc, char **argv)
{
int c;
FILE *fp;
char text_ln[80];
int i = 0;
lines_to_sort_t table[MAX];
if (argc > 1 && argv[1]) {
fp = fopen(argv[1], "r");
if (fp) {
while (fgets(text_ln, sizeof(text_ln), fp) != NULL) {
strcpy(table[i++].s_line, text_ln);
if (i >= MAX) break;
}
qsort(table, i, sizeof table[0], compare);
i = 0;
while (i < 3) {
printf("%d: %s", i, table[i].s_line);
i++;
}
}
}
return 0;
}
int compare(const void *arg1, const void *arg2)
{
const lines_to_sort_t *p1 = arg1;
const lines_to_sort_t *p2 = arg2;
return strcmp(p1->s_line, p2->s_line);
}
/*
C:\tmp>type l.txt
key_column_usage
sql_languages
catalogs
procedure_columns
procedure_parameters
procedures
check_constraints
check_constraints_by_table
foreign_keys
schemata
referential_constraints
assertions
indexes
usage_privileges
collations
table_constraints
table_privileges
table_statistics
tables
tables_info
primary_keys
dbinfoliterals
translations
character_sets
trustee
constraint_column_usage
constraint_table_usage
statistics
column_domain_usage
column_privileges
columns
provider_types
view_column_usage
view_table_usage
views

C:\tmp>foo l.txt
0: assertions
1: catalogs
2: character_sets
*/

zavnobih@gmail.com
Guest
Posts: n/a

 03-21-2007
On 21 Mrz., 02:50, "user923005" <(E-Mail Removed)> wrote:
> /*
> Some clues found below.
> I made things a bit bigger just so I wouldn't have to worry about
> the string size of items in files I had laying around.
> */
> #include <stdio.h>
> #include <stdlib.h>
> #include <assert.h>
> #include <string.h>
>
> #define MAX 100
>
> int compare(const void *arg1, const void *arg2);
>
> typedef struct {
> char s_line[80];
>
> } lines_to_sort_t;
>
> int main(int argc, char **argv)
> {
> int c;
> FILE *fp;
> char text_ln[80];
> int i = 0;
> lines_to_sort_t table[MAX];
> if (argc > 1 && argv[1]) {
> fp = fopen(argv[1], "r");
> if (fp) {
> while (fgets(text_ln, sizeof(text_ln), fp) != NULL) {
> strcpy(table[i++].s_line, text_ln);
> if (i >= MAX) break;
> }
> qsort(table, i, sizeof table[0], compare);
> i = 0;
> while (i < 3) {
> printf("%d: %s", i, table[i].s_line);
> i++;
> }
> }
> }
> return 0;}
>
> int compare(const void *arg1, const void *arg2)
> {
> const lines_to_sort_t *p1 = arg1;
> const lines_to_sort_t *p2 = arg2;
> return strcmp(p1->s_line, p2->s_line);}
>
> /*
> C:\tmp>type l.txt
> key_column_usage
> sql_languages
> catalogs
> procedure_columns
> procedure_parameters
> procedures
> check_constraints
> check_constraints_by_table
> foreign_keys
> schemata
> referential_constraints
> assertions
> indexes
> usage_privileges
> collations
> table_constraints
> table_privileges
> table_statistics
> tables
> tables_info
> primary_keys
> dbinfoliterals
> translations
> character_sets
> trustee
> constraint_column_usage
> constraint_table_usage
> statistics
> column_domain_usage
> column_privileges
> columns
> provider_types
> view_column_usage
> view_table_usage
> views
>
> C:\tmp>foo l.txt
> 0: assertions
> 1: catalogs
> 2: character_sets
> */

Thanx,
Magical "i" solved the problem.

user923005
Guest
Posts: n/a

 03-21-2007
On Mar 20, 6:59 pm, (E-Mail Removed) wrote:
> On 21 Mrz., 02:50, "user923005" <(E-Mail Removed)> wrote:
>
>
>
>
>
> > /*
> > Some clues found below.
> > I made things a bit bigger just so I wouldn't have to worry about
> > the string size of items in files I had laying around.
> > */
> > #include <stdio.h>
> > #include <stdlib.h>
> > #include <assert.h>
> > #include <string.h>

>
> > #define MAX 100

>
> > int compare(const void *arg1, const void *arg2);

>
> > typedef struct {
> > char s_line[80];

>
> > } lines_to_sort_t;

>
> > int main(int argc, char **argv)
> > {
> > int c;
> > FILE *fp;
> > char text_ln[80];
> > int i = 0;
> > lines_to_sort_t table[MAX];
> > if (argc > 1 && argv[1]) {
> > fp = fopen(argv[1], "r");
> > if (fp) {
> > while (fgets(text_ln, sizeof(text_ln), fp) != NULL) {
> > strcpy(table[i++].s_line, text_ln);
> > if (i >= MAX) break;
> > }
> > qsort(table, i, sizeof table[0], compare);
> > i = 0;
> > while (i < 3) {
> > printf("%d: %s", i, table[i].s_line);
> > i++;
> > }
> > }
> > }
> > return 0;}

>
> > int compare(const void *arg1, const void *arg2)
> > {
> > const lines_to_sort_t *p1 = arg1;
> > const lines_to_sort_t *p2 = arg2;
> > return strcmp(p1->s_line, p2->s_line);}

>
> > /*
> > C:\tmp>type l.txt
> > key_column_usage
> > sql_languages
> > catalogs
> > procedure_columns
> > procedure_parameters
> > procedures
> > check_constraints
> > check_constraints_by_table
> > foreign_keys
> > schemata
> > referential_constraints
> > assertions
> > indexes
> > usage_privileges
> > collations
> > table_constraints
> > table_privileges
> > table_statistics
> > tables
> > tables_info
> > primary_keys
> > dbinfoliterals
> > translations
> > character_sets
> > trustee
> > constraint_column_usage
> > constraint_table_usage
> > statistics
> > column_domain_usage
> > column_privileges
> > columns
> > provider_types
> > view_column_usage
> > view_table_usage
> > views

>
> > C:\tmp>foo l.txt
> > 0: assertions
> > 1: catalogs
> > 2: character_sets
> > */

>
> Thanx,
> Magical "i" solved the problem.- Hide quoted text -
>

There were some other, equally serious problems I fixed as well.
Did you notice (for instance) the check to see if the file pointer
existed before using it?

zavnobih@gmail.com
Guest
Posts: n/a

 03-21-2007
On 21 Mrz., 03:03, "user923005" <(E-Mail Removed)> wrote:
> On Mar 20, 6:59 pm, (E-Mail Removed) wrote:
>
>
>
> > On 21 Mrz., 02:50, "user923005" <(E-Mail Removed)> wrote:

>
> > > /*
> > > Some clues found below.
> > > I made things a bit bigger just so I wouldn't have to worry about
> > > the string size of items in files I had laying around.
> > > */
> > > #include <stdio.h>
> > > #include <stdlib.h>
> > > #include <assert.h>
> > > #include <string.h>

>
> > > #define MAX 100

>
> > > int compare(const void *arg1, const void *arg2);

>
> > > typedef struct {
> > > char s_line[80];

>
> > > } lines_to_sort_t;

>
> > > int main(int argc, char **argv)
> > > {
> > > int c;
> > > FILE *fp;
> > > char text_ln[80];
> > > int i = 0;
> > > lines_to_sort_t table[MAX];
> > > if (argc > 1 && argv[1]) {
> > > fp = fopen(argv[1], "r");
> > > if (fp) {
> > > while (fgets(text_ln, sizeof(text_ln), fp) != NULL) {
> > > strcpy(table[i++].s_line, text_ln);
> > > if (i >= MAX) break;
> > > }
> > > qsort(table, i, sizeof table[0], compare);
> > > i = 0;
> > > while (i < 3) {
> > > printf("%d: %s", i, table[i].s_line);
> > > i++;
> > > }
> > > }
> > > }
> > > return 0;}

>
> > > int compare(const void *arg1, const void *arg2)
> > > {
> > > const lines_to_sort_t *p1 = arg1;
> > > const lines_to_sort_t *p2 = arg2;
> > > return strcmp(p1->s_line, p2->s_line);}

>
> > > /*
> > > C:\tmp>type l.txt
> > > key_column_usage
> > > sql_languages
> > > catalogs
> > > procedure_columns
> > > procedure_parameters
> > > procedures
> > > check_constraints
> > > check_constraints_by_table
> > > foreign_keys
> > > schemata
> > > referential_constraints
> > > assertions
> > > indexes
> > > usage_privileges
> > > collations
> > > table_constraints
> > > table_privileges
> > > table_statistics
> > > tables
> > > tables_info
> > > primary_keys
> > > dbinfoliterals
> > > translations
> > > character_sets
> > > trustee
> > > constraint_column_usage
> > > constraint_table_usage
> > > statistics
> > > column_domain_usage
> > > column_privileges
> > > columns
> > > provider_types
> > > view_column_usage
> > > view_table_usage
> > > views

>
> > > C:\tmp>foo l.txt
> > > 0: assertions
> > > 1: catalogs
> > > 2: character_sets
> > > */

>
> > Thanx,
> > Magical "i" solved the problem.- Hide quoted text -

>
> There were some other, equally serious problems I fixed as well.
> Did you notice (for instance) the check to see if the file pointer
> existed before using it?

Yes, I noticed. Thanx.
Code is only a draft. Final version will be more complex.