![]() |
| | #1 |
| Registered User Join Date: May 2009
Posts: 7
| 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 | |
| | #2 |
| +++ OK NO CARRIER 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 | |
| | #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*/
}
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 | |
| | #4 | |
| Registered User Join Date: May 2009
Posts: 7
| Quote:
| |
| loopymoo26 is offline | |
| | #5 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
-- 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 | |
| | #6 |
| Registered User Join Date: May 2009
Posts: 7
| |
| loopymoo26 is offline | |
| | #7 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
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 | |
| | #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 | |
| | #9 |
| +++ OK NO CARRIER 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 | |
| | #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 | |
| | #11 | |
| and the hat of Jobseeking 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:
__________________ 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 | |
| | #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 | |
| | #13 | |
| Registered User Join Date: May 2009
Posts: 7
| Quote:
| |
| loopymoo26 is offline | |
![]() |
| Tags |
| c program |
| Thread Tools | |
| Display Modes | |
|
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 |