sorting through a struct

This is a discussion on sorting through a struct within the C Programming forums, part of the General Programming Boards category; Hi there I have a problem doing a sorting of my struct. In my code it scans the file "riget_version3", ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    17

    sorting through a struct

    Hi there

    I have a problem doing a sorting of my struct.
    In my code it scans the file "riget_version3", and stores its data in 'cprnr', 'prio' and 'skade' in my struct, and prints it out.

    The data stored and printed is like this:

    1111111111 1 F
    2222222222 4 A
    3333333333 2 C
    and so on...

    I want to sort this information by the variable 'prio'(short for priority), which has the number 1-4, where 1 is the highest priority.

    How can I do this?

    HERE IS MY CODE:
    Code:
    #include <stdio.h>
    
    typedef struct
    {
      char cprnr[10];
      int  prio;
      char skade;
    }Patient;
    
    int main()
    {
      FILE *f = popen("./riget_version3", "r");
      Patient p;
    
      if(!f)
      {
         fprintf(stderr, "Failed to open riget_version3\n");
         return 0;
      }
    
      while(fscanf(f, "%[^\t]\t%d\t%c\n", p.cprnr, &p.prio, &p.skade) > 0)
      {
    	  /* Process Info */
         printf("%s %d %c\n", p.cprnr, p.prio, p.skade);
      }
      return 0;
    }
    Thanks a lot
    Philip

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > FILE *f = popen("./riget_version3", "r");
    Is this a file, or a program?
    Use fopen for files, use popen for reading the output of programs.

    As for sorting, first you need to read the data into an array.
    Then search the board for many examples of qsort()

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    riget_version3 is a program, which stream these data out.

    okay, but how do I make this array you are talking about?

    and is qsort() the simplest way of doing it

    thanks!

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX  1000
    
    typedef struct
    {
      char cprnr[10];
      int  prio;
      char skade;
    }Patient;
    
    int main()
    {
      FILE *f = popen("./riget_version3", "r");
      Patient p[MAX];
      int num = 0;
    
      if(!f)
      {
         fprintf(stderr, "Failed to open riget_version3\n");
         return 0;
      }
    
      while( num < MAX && 
             fscanf(f, "%[^\t]\t%d\t%c\n", p[num].cprnr, &p[num].prio, &p[num].skade) == 3 )
      {
        num++;
        // printf("%s %d %c\n", p.cprnr, p.prio, p.skade);
      }
      // invoke qsort here
      return 0;
    }
    > and is qsort() the simplest way of doing it
    Well all you need to define is a compare function, to compare two patients to determine the sort order.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    okay, but how do I make the printf work again, so that I can see the output which is going to the struct, on the screen?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Well uncomment it, and make the parameters reference the array.
    Then increment num

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    The code below doesnt work for me.. what am I doing wrong?

    Code:
     while( num < MAX && fscanf(f, "%[^\t]\t%d\t%c\n", p[0].cprnr, &p[num].prio, &p[num].skade) > 0)
      {
    	  /* Process Info */
    		 num++;
         printf("\nCprnr: %s\tPrioritet: %d\t Skadesart: %c\n", p[num].cprnr, p[num].prio, p[num].skade);

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    Also the '== 3' in the while, is replaced with the '> 0', or else I dont get any output

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Increment num AFTER you print it.

    > Also the '== 3' in the while, is replaced with the '> 0', or else I dont get any output
    Perhaps your conversion is less that successful then.
    You should get 3 if all is well.
    >0 just means something happened, not that it was all successful.

  10. #10
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    thanks, that solved it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct Pointer Problems. (Warning: Long Post.)
    By Phoenix940 in forum C Programming
    Replies: 1
    Last Post: 11-30-2008, 09:04 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. Help please im stuck
    By ItsMeHere in forum C Programming
    Replies: 7
    Last Post: 06-15-2006, 04:07 AM
  5. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 05:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21