C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-22-2009, 02:40 AM   #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");

}
loopymoo26 is offline   Reply With Quote
Old 05-22-2009, 03:33 AM   #2
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,639
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.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 05-22-2009, 03:48 AM   #3
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 05-22-2009, 04:02 AM   #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
loopymoo26 is offline   Reply With Quote
Old 05-22-2009, 04:24 AM   #5
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 05-22-2009, 04:37 AM   #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
loopymoo26 is offline   Reply With Quote
Old 05-22-2009, 04:43 AM   #7
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 05-22-2009, 04:48 AM   #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
loopymoo26 is offline   Reply With Quote
Old 05-22-2009, 05:58 AM   #9
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,639
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.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 05-22-2009, 06:24 AM   #10
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 05-22-2009, 06:36 AM   #11
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,676
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
Quote:
``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.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.


Last edited by Salem; 05-22-2009 at 06:38 AM. Reason: more RTFM information
Salem is offline   Reply With Quote
Old 05-22-2009, 07:10 AM   #12
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 05-23-2009, 07:38 AM   #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
loopymoo26 is offline   Reply With Quote
Reply

Tags
c program

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Something about probablility mike_g General Discussions 116 03-13-2008 05:33 PM
arrays with elements bradleyd C Programming 5 04-10-2007 12:00 PM
how can I re-sort a map indigo0086 C++ Programming 8 06-01-2006 06:21 AM
Fastest STL container? Shakti C++ Programming 18 02-17-2006 02:07 AM
Problems while reading arguments Lost__Soul C Programming 6 05-06-2003 01:02 AM


All times are GMT -6. The time now is 03:04 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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