Problem by sorting array of structures using qsort

Z

zavnobih

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.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.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);


}
 
E

Eric Sosman

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!
 
Z

zavnobih

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!

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?

Adnan Selimovic.
 
U

user923005

/*
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.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
*/
 
Z

zavnobih

/*
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.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.
 
U

user923005

/*
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.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?
 
Z

zavnobih

/*
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.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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top