Thread: Problems reading entered race times C

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    7

    Post Problems reading entered race times C

    Whenever I enter a race time say 3.20 (for 3hrs 20 mins), when I go to produce the race report it brings up the time as 0.000000

    What am I doing wrong?

    Here is the code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <float.h>
    typedef struct
      {
        char fname [15] ;
        char sname [35];
        int age ;
        char category [2];
        int number;
        float time;
    }
    competitor_details;
    
    //Global Variables
    
    FILE *compfile;
    competitor_details comp;
    int choice=0;
    
    //Declare Functions
    
    void menu (void);
    void entercompetitordetails(void);
    void producecompetitordetails(void);
    void enterracetimes(void);
    void produceracereport(void);
    
    int main(int argc, char *argv[])
    {
        menu();
      system("PAUSE");
      return 0;
      }
    
    
    void menu()
    {
    
      while (choice  !=9)
      {
      system("CLS");
      printf("                 +++++++++++++++++++++++++++++++++++++++++++\n");
      printf("                 +             Competitor details          +\n");
      printf("                 +                                         +\n");
      printf("                 +          [1] Enter competitor details   +\n");
      printf("                 +          [2] Produce competitor details +\n");
      printf("                 +          [3] Enter race times           +\n");
      printf("                 +          [4] Produce race report        +\n");
      printf("                 +          [9] Exit                       +\n");
      printf("                 +                                         +\n");
      printf("                 +++++++++++++++++++++++++++++++++++++++++++\n");
      printf("                                Enter Choice "                 );
      scanf("%i",&choice);
      printf("\n");
    
      switch (choice){
             case 1:entercompetitordetails();
             break;
             case 2:producecompetitordetails();
             break;
             case 3:enterracetimes();
             break;
             case 4:produceracereport();
             break;
             case 9:printf("exit\n");
             break;
             default : printf("It is one of the undefined values\n");
             system("PAUSE");
    		 break;
             }
      }
    }
    void entercompetitordetails(void)
    
    {
    
    system ("cls");
    
    compfile=fopen("compfile.bin", "ab");
    
    if (compfile ==0)
    {printf ("An error occurred opening file.\n");
    }
    
    
    
    
    
    
           printf ("Please enter competitor number or 0 to quit\n");
           scanf ("%i", &comp.number);
    
          while (comp.number != 0.)
            {
            printf(" Please enter first name  \n");
            scanf ("%s", &comp.fname);
            printf(" Please enter surname\n");
            scanf ("%s", &comp.sname);
            printf(" Please enter age \n");
            scanf ("%i", &comp.age);
            printf(" Please enter category (Juvenile J, standard S, Expert E} \n");
            scanf ("%s", &comp.category);
    
            fwrite(&comp, sizeof(comp),1,compfile);
    
            printf ("Please enter competitor number or 0 to quit\n");
            scanf ("%i", &comp.number);
    
            }
    
    
    
    
     fclose(compfile);
    
      system("PAUSE");
    
    
    }
    
    /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
    
    /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
    void producecompetitordetails(void)
    
    
    {
       compfile = fopen("compfile.bin", "rb"); /* Open File */
    
       if (compfile == 0)
          {
          printf ("An error occurred while opening the file.\a\n");
          printf ("Please choose option 1.\n\n");
          system("PAUSE");
          }/*End of if statment*/
       else
       {
            system ("cls");  /* Clear the Screen */
    
            printf ("              Competitor Details\n\n");
            printf ("First Name\tSurname\tAge\tCategory\t Number\n");
            while (!feof(compfile))
                  {
                  fread(&comp, sizeof(comp),1,compfile);
                  if(!feof(compfile))
                  printf ("%-20s\t%-20s\t%-2i\t%-3s\t%-3i\n", comp.fname, comp.sname, comp.age, comp.category, comp.number);
                  }/*End of while*/
    
            fclose (compfile);/*Close the file*/
            system("PAUSE");
            }}
    /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
    
    /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
    void enterracetimes(void)
    {
    
    int search_no; //local variable
    
    system ("cls");
    
    
    
         compfile = fopen("compfile.bin","ab+");
    
         if (compfile == 0)
         {
                         printf ("File not opened\a\n");
                         printf ("Please choose option 1.\n\n ");
                         system("PAUSE");
                         }
         else
         {
              printf ("Please enter competitor Number? ");
              scanf("%i", &search_no);
    
              while (!feof(compfile))
              {
              fread(&comp, sizeof(comp),1,compfile);
    
              if  (search_no == comp.number)
              {
              printf("Competitor is %s %s\n", comp.fname, comp.sname);
              printf ("Please enter competitors race time\n");
              scanf ("%f", &comp.time);
              fwrite (&comp, sizeof(comp),1,compfile);
              break;
              }
    
    
              if (search_no!=comp.number)
              printf("competitor number is not Valid");
    
        }
    
        fclose(compfile);
        system("PAUSE");}
    }
    
    /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
    
    void produceracereport(void)
    
     {
    
    compfile=fopen("compfile.bin", "rb");
    
     if (compfile == 0)
          {
          printf ("An error occurred while opening the file.\a\n");
          printf ("Please choose option 1.\n\n");
          system("PAUSE");
          }/*End of if statment*/
       else
       {
            system ("cls");  /* Clear the Screen */
    
            printf ("              LIST OF COMPETITOR RACE TIMES   ``\n\n");
            printf ("COMPETITOR No.\tFIRST NAME   \tSURNAME      \tTIME\n\n");
            while (!feof(compfile))
                  {
                  fread(&comp, sizeof(comp),1,compfile);
                  if(!feof(compfile))
                  printf ("%i       \t%-15s\t %-10s\t %f\n", comp.number,comp.fname,comp.sname,comp.time);
    
                  }/*End of while*/
                  }
            fclose (compfile);/*Close the file*/
    
      system("PAUSE");
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's a lot of crap to read for us to just assume that your input files are correct. Why aren't you using local variables for your file reads, since all you're doing is opening and closing in the same function anyway?

    Why don't you skip the whole file reading part, and make it so you hand-enter your data. Once you know it works when you type it in manually, try swapping out those input lines for file reads. If it stops working, your data is wrong, or you're calling the functions incorrectly.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
            while (!feof(compfile))
                  {
                  fread(&comp, sizeof(comp),1,compfile);
                  if(!feof(compfile))
                  printf ("%i       \t%-15s\t %-10s\t %f\n", comp.number,comp.fname,comp.sname,comp.time);
    
                  }/*End of while*/
                  }
    If you change the while(!feof(...)) into while(fread(...) == 1), you can remove the if(feof(...)) code, because you will NEVER enter the loop when you reach the EOF condition.

    It would also help if you could explain what exactly is wrong with your output.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    7
    Quote Originally Posted by matsp View Post
    Code:
            while (!feof(compfile))
                  {
                  fread(&comp, sizeof(comp),1,compfile);
                  if(!feof(compfile))
                  printf ("%i       \t%-15s\t %-10s\t %f\n", comp.number,comp.fname,comp.sname,comp.time);
    
                  }/*End of while*/
                  }
    If you change the while(!feof(...)) into while(fread(...) == 1), you can remove the if(feof(...)) code, because you will NEVER enter the loop when you reach the EOF condition.

    It would also help if you could explain what exactly is wrong with your output.

    --
    Mats
    I did explain what was wrong. When producing the Race Report after entering the race times, the race time comes up as 0.00000 instead of what I enter

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by loopymoo26 View Post
    I did explain what was wrong. When producing the Race Report after entering the race times, the race time comes up as 0.00000 instead of what I enter
    So, when you read in the competitor, and then write it back out again, are you sure you write it to the right place?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    7
    Quote Originally Posted by matsp View Post
    So, when you read in the competitor, and then write it back out again, are you sure you write it to the right place?

    --
    Mats

    If I say yeah, Im pretty sure, but I am a beginner - would that help?

    Everything else works fine, just this one little hitch

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by loopymoo26 View Post
    If I say yeah, Im pretty sure, but I am a beginner - would that help?

    Everything else works fine, just this one little hitch
    I suspect you are NOT writing to the same place, since your file position moves one record forward when you read, so when you write it out again, you are at the next entry.

    Exactly what have you done to check your time. How many competitors have you got?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    7
    competitors are being entered when required so at the moment Ive just got a couple. Ive just asked to produce a race report and when entering race time, the race report just produces 0.000

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Easy way to test it. Open the file, read each record one at a time and display the record number, and the contents of it. If it's not where you expect it to be, then you need to make sure you're adjusting your file pointer's location before writing.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I just compiled the code and tested the return value from fwrite, and it comes back with zero. I'm currently looking into why that is.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Between writing and reading a file, you need to do fflush()
    Also as matsp has noted, you need to move the file pointer back one record if you mean to overwrite the record you just read.

    So
    fseek back 1 record
    fwrite new record
    fflush file

    More problems
    1. "+" does NOT do what you want
    ``a+'' Open for reading and writing. The file is created if it does not
    exist. The stream is positioned at the end of the file. Subse-
    quent writes to the file will always end up at the then current
    end of file, irrespective of any intervening fseek(3) or similar.
    Last edited by Salem; 05-22-2009 at 06:38 AM. Reason: more RTFM information
    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.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I've made it work with the following changes:
    1. change the mode to "rb+"
    2. add fflush(compfile); before writing.
    3. add fseek(compfile, -(int)sizeof(comp), SEEK_CUR); before write.

    This is probably helping you a bit too much, but after all the effort I spent in getting those three lines right I don't want to let it disappear.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    7
    Quote Originally Posted by matsp View Post
    I've made it work with the following changes:
    1. change the mode to "rb+"
    2. add fflush(compfile); before writing.
    3. add fseek(compfile, -(int)sizeof(comp), SEEK_CUR); before write.

    This is probably helping you a bit too much, but after all the effort I spent in getting those three lines right I don't want to let it disappear.

    --
    Mats
    Thank you so much, your truly a genius

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Something about probablility
    By mike_g in forum A Brief History of Cprogramming.com
    Replies: 116
    Last Post: 03-13-2008, 05:33 PM
  2. arrays with elements
    By bradleyd in forum C Programming
    Replies: 5
    Last Post: 04-10-2007, 12:00 PM
  3. how can I re-sort a map
    By indigo0086 in forum C++ Programming
    Replies: 8
    Last Post: 06-01-2006, 06:21 AM
  4. Fastest STL container?
    By Shakti in forum C++ Programming
    Replies: 18
    Last Post: 02-17-2006, 02:07 AM
  5. Problems while reading arguments
    By Lost__Soul in forum C Programming
    Replies: 6
    Last Post: 05-06-2003, 01:02 AM

Tags for this Thread