Problem by sorting array of structures using qsort

Discussion in 'C Programming' started by zavnobih@gmail.com, Mar 21, 2007.

  1. Guest

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


    }
     
    , Mar 21, 2007
    #1
    1. Advertising

  2. Eric Sosman Guest

    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
    lid
     
    Eric Sosman, Mar 21, 2007
    #2
    1. Advertising

  3. Guest

    On 21 Mrz., 02:35, Eric Sosman <> wrote:
    > 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
    >


    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, 2007
    #3
  4. user923005 Guest

    /*
    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
    */
     
    user923005, Mar 21, 2007
    #4
  5. Guest

    On 21 Mrz., 02:50, "user923005" <> 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.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, 2007
    #5
  6. user923005 Guest

    On Mar 20, 6:59 pm, wrote:
    > On 21 Mrz., 02:50, "user923005" <> 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.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?
     
    user923005, Mar 21, 2007
    #6
  7. Guest

    On 21 Mrz., 03:03, "user923005" <> wrote:
    > On Mar 20, 6:59 pm, wrote:
    >
    >
    >
    > > On 21 Mrz., 02:50, "user923005" <> 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.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, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dimitris Mandalidis

    qsort mode-based sorting

    Dimitris Mandalidis, May 9, 2004, in forum: C Programming
    Replies:
    6
    Views:
    361
  2. sorting 2d arrays using qsort

    , Jun 11, 2005, in forum: C Programming
    Replies:
    23
    Views:
    1,604
    CBFalconer
    Jun 14, 2005
  3. Alfonso Morra
    Replies:
    11
    Views:
    721
    Emmanuel Delahaye
    Sep 24, 2005
  4. Albert

    Qsort-ing structures

    Albert, Feb 19, 2009, in forum: C Programming
    Replies:
    16
    Views:
    1,271
    Ben Bacarisse
    Mar 4, 2009
  5. Cstudent

    sorting an array of strings using qsort

    Cstudent, May 10, 2009, in forum: C Programming
    Replies:
    1
    Views:
    480
    luserXtrog
    May 10, 2009
Loading...

Share This Page