typedef problem

This is a discussion on typedef problem within the C Programming forums, part of the General Programming Boards category; Hi all, I've got a question about typdef. I want to save highscores for my schoolproject and I want to ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    4

    typedef problem

    Hi all,

    I've got a question about typdef.
    I want to save highscores for my schoolproject and I want to do it with a typedef.

    Code:
    typedef struct
         {
              char name[20];
              int hs;
         } HS;
    
    HS highscore[4];
    My program writes the highscores to a binairy file, but I don't know how to continue with the typedef...

    I tried the following already:

    Code:
    if((f = fopen("hs.bin","rb")) == NULL)
         {
              strcpy(highscore[0].name, plyr_nm);
              highscore[0].hs = score;
              f = fopen("hs.bin", "wb");
              length = strlen(highscore);
              fwrite(&length, 1,sizeof(int),f);
              fwrite(&highscore, length+1, sizeof(HS), f);
              fclose(f);
         }
         else
         {
               fopen("hs.bin", "rb");
               fread(&length, sizeof(int),1,f);
               fread(&highscore, sizeof(char),length+1,f);
               fclose(f);
               strcpy(highscore[4].name, plyr_nm);
               highscore[4].hs = score;
    
               //than sort with qsort and write it back into the file
         }
    I also don't understand qsort, I don't know how to write a comparison that works for typedef.

    Can someone help me out please?

    Greetings!

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    You should remove fopen("hs.bin", "rb"); from else
    Why don't you write/read the whole structure?

    Your Array has indexes 0,1,2,3 so highscore[4] does not exists
    if you want to write the whole array use
    fwrite(highscore,1,sizeof(highscore),f);
    if you want to write the first element
    fwrite(&highscore[0],1,sizeof(highscore[0]),f);

    etc
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    I also don't understand qsort, I don't know how to write a comparison that works for typedef.
    Use this code below as compare fucntion which u need to send as a parameter to your qsort function.

    NOTE: This function coomapre on your struct hs member value, but u can still change the code so that it compares on the name rather than to be in hs. I leave it to you for that.

    Code:
    int compare (const void *a, const void *b)
    {
        return ((*(HS *)a).hs - (*(HS *)b).hs);
    }
    And this is how u will call
    Code:
    qsort(highscore,5,sizeof(HS), compare);
    Hope that help u

    ssharish2005

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by anarchypower
    I also don't understand qsort, I don't know how to write a comparison that works for typedef.
    Code:
    int compare_highscores( const void* a, const void* b ) {
       HS* score1 = (HS*) a;
       HS* score2 = (HS*) b;
       if( score1->hs  < score2->hs ) return -1;
       else if( score1->hs == score2->hs ) return 0;
       else return 1;
     }
    Kurt

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    int compare (const void *a, const void *b)
    {
        return ((*(HS *)a).hs - (*(HS *)b).hs);
    }
    Although this is a very, very common compare function to see, it also has a very well known flaw: it has UB for integer overflow.

    And there is no need to cast away constness.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by Dave_Sinkula
    Code:
    int compare (const void *a, const void *b)
    {
        return ((*(HS *)a).hs - (*(HS *)b).hs);
    }
    Although this is a very, very common compare function to see, it also has a very well known flaw: it has UB for integer overflow.

    And there is no need to cast away constness.
    Dave true. I never through abut that. Well Zuks solution looks fine for the comparison.

    But what i don't understand is why cast is not required. The reason why i casted was because the value which we get from the compare in of type void * which need casting to get member data types.

    ssharish2005

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by ssharish2005
    The reason why i casted was because the value which we get from the compare in of type void * which need casting to get member data types.
    You don't need casting because it's a void*.

    I don't understand the following line of code
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by Dave_Sinkula
    You don't need casting because it's a void*.

    I don't understand the following line of code
    one thing which i dont understand is, when i was tried as suggested without casting a the const void *. I get these error. Do you why i cant do as u said.

    Code:
    testcom.c: In function &#226;compare&#226;:
    testcom.c:12: warning: dereferencing &#226;void *&#226; pointer
    testcom.c:12: error: request for member &#226;hs&#226; in something not a structure or union
    testcom.c:12: warning: dereferencing &#226;void *&#226; pointer
    testcom.c:12: error: request for member &#226;hs&#226; in something not a structure or union
    i am using gcc 4.1.1 version

    ssharish2005

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    > when i was tried as suggested without casting a the const void *. I get these error.
    You still have to assign the void pointers to pointers of the correct type.
    But you can do this without the need for explicit casts.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. typedef problem
    By dr$brown in forum C Programming
    Replies: 6
    Last Post: 12-25-2004, 11:27 AM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 06:35 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

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