Thread: file printing problem!

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    6

    file printing problem!

    Hi all,

    I'm writing information in a .bin and I want to print it back onto the screen. I think the writing works pretty fine, but when I try to print....weird characters appear...I have no compile errors and I don't know how to solve this.
    There are only 3 highscores. They get sorted by a qsort function, but I don't know why it's printing funny characters!


    Below is the code:

    Code:
    //high Scores
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    
    int compare(const void*, const void*);
    
    typedef struct
    {
               char name[20];
               int hs;
    } HS;
    
    void show_hs(void)
    {
              FILE *f;
              int length,i;
              HS highscore[4];
              if((f = fopen("hs.bin","rb")) == NULL)
              {
              printf("There are no High Scores");
              }
              else
              {
                        fopen("hs.bin", "rb");
                        fread(&length, sizeof(int),1,f);
                        fread(&highscore, sizeof(char),length+1,f);
                        fclose(f);
                        printf("Highscores: \n");
                        for(i=0;i<2;i++)
                        {
                        printf("%d: %s - %d",i+1,highscore[i].name,highscore[i].hs);
                        }
              }
    }
    
    
    
    
    void add_hs(int score)
    {
               FILE *f;
               int res, length;
               char plyr_nm[20];
               char c;
               int sw_ok = 0;
    
    
               HS highscore[4];
               while(!sw_ok)
               {
                                         printf("Please enter you name. (Max 3 characters long.\n");
                                         res = scanf("%s%c", &plyr_nm, &c);
                                         if (res != 2 || c != '\n' || strlen(plyr_nm) != 3 )
                                         {
                                                       printf("%d  %d",res,strlen(plyr_nm));
                                                       printf("Illegal input!\n");
                                                       printf("Try again : \n");
                                         }
                                         else
                                         {
                                                       sw_ok = 1;
                                         }
               }
    
               if((f = fopen("hs.bin","rb")) == NULL)
               {
                             strcpy(highscore[0].name, plyr_nm);
                             highscore[0].hs = score;
                             f = fopen("hs.bin", "wb");
                             length = 20;
                             fwrite(&length,sizeof(int),1,f);
                             fwrite(&highscore,sizeof(HS),length+1,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;
                              qsort(highscore,4,sizeof(HS),compare);   //Sort the highscores
                              f = fopen("hs.bin", "wb");
                              length = 20;
                              fwrite(&length,sizeof(int),1,f);
                              fwrite(&highscore,sizeof(HS),lengte+1,f);
                              fclose(f);
               }
    
    
    
    
    }
    
    
    int compare( 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;
    }

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    fread(&highscore, sizeof(char),length+1,f);

    What you want here is probably something more like :

    fread(&highscore, sizeof(HS),length+1,f);


    Also you don't need to make all those fopen calls.

    Example :

    Code:
    if((f = fopen("hs.bin","rb")) == NULL)
              {
              printf("There are no High Scores");
              }
              else
              {
                        fopen("hs.bin", "rb");  <---  Unnecessary call.
                        fread(&length, sizeof(int),1,f);
                        fread(&highscore, sizeof(char),length+1,f);
                        fclose(f);
                        printf("Highscores: \n");
                        for(i=0;i<2;i++)
                        {
                        printf("%d: %s - %d",i+1,highscore[i].name,highscore[i].hs);
                        }
              }
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    where is main? (will make much easier to debug)
    is length synonym to number_of_players?
    why length = 20? (it's pointless because of what comes next)

    you will need malloc(size_t); to achieve what you want.
    you will need to do bound checking because according to the definition (HS highscore[4] indexs higher than 3 cannot be used.
    watch your spelling of length i see lengte in add_hs near the end.

    following on from Happy_Reaper fopen(const char *, const char *); does nothing by itself you need to assign it to something.
    Last edited by peterchen; 01-06-2007 at 08:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. Problem reading file
    By coder_009 in forum C Programming
    Replies: 10
    Last Post: 01-15-2008, 01:22 PM
  4. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM