I'm rather new to C and my previous experience with linked lists are limited and this is driving me nuts.
Below I have posted all parts of the program that are troubling me. I believe that the function enterBoats() works perfectly (but if you happen to notice errors please let me know). The problem lies with createMaster() I believe it is a pointer problem but I've been looking at this crap so long I've gone nuts.
What the program is meant to do is grab a bunch of strings from a file store them in a linked list. Then at this point it creates another file a prints them into it.
If you need any more information please ask, this is very quickly becoming an emergance to write. I know it looks like a big problem but I think it's problem something simple. Any help and suggestions would be fantastic.
Thanks in advance
Kris
/** HEADER FILE **/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define MaxRaces 5
#define MaxName 30
#define maxHours 999
#define maxMinutes 59
#define maxSeconds 59
#define inPutFile argv[1]
#define opFile argv[2]
#define masterFile "rec"
FILE *masterFp;
enum BoatType {Gwen, Skiff16, Thorpe12};
typedef enum BoatType BoatType;
typedef struct {
unsigned short Hours;
unsigned short Minutes;
unsigned short Seconds;
} Time;
typedef char Digit; /* only contains '0'..'9' */
typedef Digit Sail[3+1];
typedef char Name[MaxName+1];
typedef struct RaceTime {
Time Duration;
struct RaceTime *Next;
} RaceTime, *timePtr;
typedef struct Boat {
Sail SailNum;
Name BoatName;
RaceTime Elapsed;
} Boat;
typedef unsigned PlaceArray[MaxRaces];
typedef struct ResultRec {
Boat Competitor;
BoatType Class;
PlaceArray Place;
} ResultRec;
typedef struct boatList {
ResultRec boatDetails;
struct boatList *next;
} boatList, *boatPtr;
timePtr newTimePtr, timeCurrent, timeHead, newTPtr;
boatPtr firstNode, lastNode;
/******* Function Prototypes *******/
int enterBoats (FILE *);
void operate (FILE *, int);
void initialise (boatPtr *);
boatPtr allocate (void);
void print (boatPtr, int);
void createMaster (int);
void printMaster (int);
void updateRace (char *);
void seriesStandings ();
void queryStandings (char *, int);
void errorMsg (int);
int makeInt (char *);
BoatType convertMake (char *);
/*char * upCase (char *);*/
/** FUNCTIONS **/
#include "boatDetails.h"
int main(int argc, char *argv[])
{
FILE *inPutFp, *opFp; /* Assign file pointers */
int records;
firstNode = lastNode = NULL;
inPutFp = fopen (inPutFile, "r");
records = enterBoats (inPutFp);
close (inPutFp);
fflush (stdin);
createMaster(records);
return 0;
}
boatPtr allocate ( void ) {
boatPtr ptr;
ptr = (boatPtr)malloc(sizeof(boatList));
ptr->next = 0;
ptr->boatDetails.Competitor.Elapsed.Next = 0;
return ptr;
}
timePtr allocateTime ( void ) {
timePtr ptr;
ptr = (timePtr)malloc(sizeof(RaceTime));
ptr->Next = NULL;
return ptr;
}
int enterBoats (FILE * inPutFp)
{
Sail inSail;
Name inName;
timePtr pTime;
char inBoatType[16];
int i, records = 0;
while(!feof(inPutFp))
{
if (fscanf (inPutFp, "%s %s %s", inSail, inName, inBoatType) == 3)
{
records++;
lastNode = allocate();
printf("Boats in variables = %s %s %s\n",inSail, inName, inBoatType);
strcpy(lastNode->boatDetails.Competitor.SailNum, inSail);
strcpy(lastNode->boatDetails.Competitor.BoatName, inName);
if (strcmp (inBoatType, "Gwen") == 0) lastNode->boatDetails.Class = Gwen;
else if (strcmp (inBoatType, "Skiff16") == 0) lastNode->boatDetails.Class = Skiff16;
else lastNode->boatDetails.Class = Thorpe12;
printf ("Boats in linked list = %s\n\n", lastNode->boatDetails.Competitor.BoatName);
pTime=&(lastNode->boatDetails.Competitor.Elapsed);
pTime->Duration.Hours = maxHours;
for (i = 0; i < 4; i++) {
pTime=pTime->Next=allocateTime();
pTime->Duration.Hours = i;
}
if (firstNode == NULL)
firstNode = lastNode;
}
}
return records;
}
void createMaster (int records) {
boatPtr head;
Sail inSail;
Name inName;
timePtr pTime;
char inBoatType[16];
boatPtr current, boatptr;
timePtr newPtr;
RaceTime racePtr;
int j, i; char boatNum[20], boatName[20], boatType[20];
masterFp = fopen (masterFile, "w");
current = firstNode;
while (current != NULL) {
if (current->boatDetails.Class == 0)
strcpy (boatType, "Gwen");
else if (current->boatDetails.Class == 1)
strcpy (boatType, "Skiff16");
else if (current->boatDetails.Class == 2)
strcpy (boatType, "Thorpe12");
fprintf(masterFp, "%s ", boatType);
fprintf(masterFp, "%s ",current->boatDetails.Competitor.SailNum);
fprintf(masterFp, "%s ",current->boatDetails.Competitor.BoatName);
for (i = 0; i < 5; i++) {
fprintf(masterFp, " %d ", current->boatDetails.Competitor.Elapsed.Duration.Hours);
fprintf(masterFp, "%d ", current->boatDetails.Competitor.Elapsed.Duration.Minutes );
fprintf(masterFp, "%d", current->boatDetails.Competitor.Elapsed.Duration.Seconds );
current->boatDetails.Competitor.Elapsed.Next;
}
fprintf(masterFp, "\n");
printf("Boats in create master = %s ",current->boatDetails.Competitor.SailNum);
printf("%s \n",current->boatDetails.Competitor.BoatName);
current = current->next; */
}