There are many strange things. First thing is, it seems like the names are in a file, but what you are doing is in every iteration of your for loop, you call get_names which opens the file again, that is goes to the beginning of the file and you read the same name over and over again.
That is if you closed the file. Since you haven't closed the file, the file is already open and you keep reopening it (which could be the cause of your problem)
Another thing is, how can
if (status == EOF) { count = MAX_PLAYRS; }
Give you the current count? Regardless of the count of the players in the file, you are just setting it to MAX_PLAYERS.
Another thing is that count is an input to the function that is not a pointer, so setting it does not change the value outside the function (which is what I assumed you wanted).
Here is how I would do it with minimum change to your code:
#include <stdio.h> #define MAX_NAME 20 #define MAX_PLAYRS 16 typedef struct { char pname[MAX_NAME]; int runs; char how_out; } Team_t; Team_t player[MAX_PLAYRS]; Team_t *player_ptr[MAX_PLAYRS]; void get_names (int count, char *str, FILE *inp); int main (void) { FILE *inp; int i; int count; inp = fopen("teamnames.rtf", "r"); for (i = 0; i < MAX_PLAYRS; i++) { get_names(&count, player[i].pname, inp); printf("Player: %s\n", player[i].pname); } } void get_names (int *count, char *str) { char status; status = fscanf(inp, "%s", str); if (status == EOF) { *count = MAX_PLAYRS; } }
Here is how I would do it more concisely:
#include <stdio.h> #define MAX_NAME 20 #define MAX_PLAYRS 16 typedef struct { char pname[MAX_NAME]; int runs; char how_out; } Team_t; Team_t player[MAX_PLAYRS]; Team_t *player_ptr[MAX_PLAYRS]; int get_names (Team_t *team); int main (void) { get_names(player); } int get_names (Team_t *team) { int i = 0; FILE *inp; inp = fopen("teamnames.rtf", "r"); while (i < MAX_PLAYRS && !feof(inp) { fscanf(inp, "%s", team[i].pname); printf("Player: %s\n", player[i].pname); } }
Note that the problems with fscanf, checking array boundaries etc are not the concern of this solution, but this rather gives you the idea of what to do not a code for you to copy-paste.
%19s, ...