Hello all,
Ok, I'm trying to make a program that plays a soccer league using random number generators to allocate goals to playing sides. That is all working without any problems. My problem is, when I try to update the file, it just overrides the file with nothing! In my program I am trying to update the team statisitics after a round of games has been played. This is a simplified version of what I am trying to do...
in my functions.h file...
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct players{
char *familyName;
char *firstName;
int playerNo;
int noSeasonsPlayed;
int noGamesPlayed;
int noGoals;
int worth;
int ID;
};
typedef struct players PlayerInfo;
void displayPlayerInfoFamily (PlayerInfo [], int);
void outputFile(FILE *, PlayerInfo [], int);
int readFilePlayers ( FILE *, PlayerInfo []);
This is my functions.c file....
Code:
#include "functions.h"
int readFilePlayers(FILE *PdataR, PlayerInfo players[]) {
char familyName[20];
char firstName[20];
int read = 0;
fscanf(PdataR, "%s", familyName);
while (read < 3 && !feof(PdataR)) {
players[read].familyName = malloc (sizeof(char) * (strlen(familyName) + 1));
strcpy(players[read].familyName, familyName);
fscanf(PdataR,"%s", firstName);
players[read].firstName = malloc (sizeof(char) * (strlen(firstName) + 1));
strcpy(players[read].firstName, firstName);
fscanf(PdataR, "%d %d %d %d %d %d", &players[read].playerNo, &players[read].noSeasonsPlayed, &players[read].noGamesPlayed,
&players[read].noGoals, &players[read].worth, &players[read].ID);
read++;
fscanf(PdataR, "%s", familyName);
}
fclose(PdataR);
return read;
} //End of readFilePlayers function
void displayPlayerInfoFamily (PlayerInfo players[], int noOfPlayers) {
int print = 0;
char familyNameIn[22];
printf("Please enter the surname of the player: ");
scanf("%s", &familyNameIn);
printf("\n");
for (print = 0; print < noOfPlayers; print++) {
if (strcmp(familyNameIn, players[print].familyName) == 0) {
printf("Player statistics \n");
printf("==================================================\n");
printf("||Surname: %22s \n", players[print].familyName);
printf("||Initial: %17s \n", players[print].firstName);
printf("||Player Position:%10d \n",players[print].playerNo);
printf("||Seasons Played: %10d \n", players[print].noSeasonsPlayed);
printf("||Games Played: %12d \n", players[print].noGamesPlayed);
printf("||Number of goals scored: %2d \n", players[print].noGoals);
printf("||Worth ($M): %14d \n", players[print].worth);
printf("||Player ID:%17d \n", players[print].ID);
printf("||________________________________________________\n");
}
}
printf("\n");
}
void outputFile(FILE *PdataW, PlayerInfo players[], int noOfPlayers) {
/* Runs when the user wishes to exit your program */
int write = 0;
printf("HERE1!\n");
/* loop control, used for sending information to the file */
if (PdataW == NULL) {
printf("HERE1!\n");
/* checks if the file exists */
printf("Error, this file does not exist!");
exit(0);
}
else {
/* if it does... */
printf("HERE2!\n");
printf("noOfPlayers is: %d", noOfPlayers);
printf("write is: %d", write);
while(write < noOfPlayers && !feof(PdataW)) {
/* Prints the array information to the file */
printf("HERE1!\n");
fprintf(PdataW,"%s", players[write].familyName);
printf("HERE3\n");
fprintf(PdataW, " ");
printf("HERE4\n");
/* This is so there is always a space between the player's first and last name, no matter what the size */
fprintf(PdataW,"%s\n", players[write].firstName);
printf("HERE5\n");
fprintf(PdataW,"%-d %-d %-d %-d %-d %-d\n", players[write].playerNo, players[write].noSeasonsPlayed,
players[write].noGamesPlayed, players[write].noGoals, players[write].worth, players[write].ID);
printf("HERE6\n");
write++;
printf("write is: %d", write);
/* updating the array position */
} // end while
} // end else
printf("HERE7\n");
} // end of outputFile function
this is my main.c....
Code:
#include "functions.h"
int main () {
FILE *PdataR = fopen("players.txt", "r");
FILE *PdataW = fopen("players.txt", "w");
PlayerInfo players[2];
int noOfPlayers;
noOfPlayers = readFilePlayers(PdataR, players);
printf("%d", noOfPlayers);
displayPlayerInfoFamily(players, noOfPlayers);
}
I know that this does not ask me to enter a new name or value or anything, but it is just wiping the text file before it even reads it in. where do I have to close PdataR and open RdataW?
Cheers,
Submeg