By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,634 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

Problem by sorting array of structures using qsort

P: n/a
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);
}

Mar 21 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
za******@gmail.com 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
es*****@acm-dot-org.invalid
Mar 21 '07 #2

P: n/a
On 21 Mrz., 02:35, Eric Sosman <esos...@acm-dot-org.invalidwrote:
zavno...@gmail.com 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
esos...@acm-dot-org.invalid
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.

Mar 21 '07 #3

P: n/a
/*
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
*/

Mar 21 '07 #4

P: n/a
On 21 Mrz., 02:50, "user923005" <dcor...@connx.comwrote:
/*
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.

Mar 21 '07 #5

P: n/a
On Mar 20, 6:59 pm, zavno...@gmail.com wrote:
On 21 Mrz., 02:50, "user923005" <dcor...@connx.comwrote:


/*
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?
Mar 21 '07 #6

P: n/a
On 21 Mrz., 03:03, "user923005" <dcor...@connx.comwrote:
On Mar 20, 6:59 pm, zavno...@gmail.com wrote:
On 21 Mrz., 02:50, "user923005" <dcor...@connx.comwrote:
/*
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.

Mar 21 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.