Question: Player.c Code : #include #include /* player struct */ struct player { /* lab exercise: define member variables here */ }; typedef struct player player;

 Player.c Code : #include #include /* player struct */ struct player

{ /* lab exercise: define member variables here */ }; typedef struct

Player.c Code :

#include #include

/* player struct */ struct player { /* lab exercise: define member variables here */

};

typedef struct player player;

/* returns the index of the player in game_state that has the * highest score; if there is a tie, the lowest index is * returned */ int find_max(player * game_state, int len) { int i = 0; int to_return = 0; if(len

return to_return; }

/* usage: player * reads game player state from input file, finds player with * highest score and writes the player's information to the * output file name */ int main(int argc, char * argv[]) { // check that there are exactly 3 command-line arguments if(argc != 3) { fprintf(stderr, "Usage: player input_file_name output_file_name. Exiting program. "); return 1; //error code } // open file and check for null FILE* fp = fopen(argv[1], "r"); if(fp == NULL) { fprintf(stderr, "File cannot be opened. "); return 1; //error code }

// read first line size_t bytes_read = 0; ssize_t num_bytes = 0; char *first_line = NULL;

bytes_read = getline(&first_line, &num_bytes, fp); if(bytes_read == -1) { fprintf(stderr, "First line of file is invalid. Exiting program "); return 1; }

// convert string to int int count = atoi(first_line); // read rest of file: file contents are (per line) // name // x_loc // y_loc // points // // note: should really error check each call to getline // to exit gracefully, but for the purpose of this lab, // we are keeping the code short

player * game_state = malloc(sizeof(player)*count); int i; char * name_read = NULL; char * x_loc_read = NULL; char * y_loc_read = NULL; char * points_read = NULL; for(i=0; i

// find player with highest score int max = find_max(game_state, count);

// print the player's information to an output file FILE *out;

/* lab exercise: open argv[2] for writing and set it to out */

if(out == NULL) { fprintf(stderr, "Cannot open high_score.txt for writing. "); return 1; //error code }

/* lab exercise: write highest score player to output file here */ /* use fprintf to print to file instead of printf */

// close output file fclose(out); return EXIT_SUCCESS; }

----------------------------------------------

index.txt content :

5 John 3 -10 500 Sally 40 20 1000 Jim 30 10 -400 Richard 0 50 20 Jill 5000 2 60

Part 3: Defining a struct, reading from a text file, writing to a text file (switch driver) 1. Compile player.c: gcc-o player player.c What error messages do you get? What do you think needs to be defined in player.c? 2. Open player.c in emacs. Examine the code. In general, this program: opens an input file that stores the game state (players); think of this as loading a saved game state into a video game stores the data from the input file into an malloc'ed array of player objects finds the player with the most points writes the player's data with the most points to the output file 3. First, define the player struct so it has the following members: name (char *) x loc (int) y_loc (int) points (int) Once you have finished the struct definition, try to compile it again: gcc-o player player.c Hopefully, you now have a compiled program, but it is not fully functional. 4. Complete the function find max. It takes an array of player objects and thee length of the array as parameters. It should return the index of the player who has the most points. Part of the function is already completed for you

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!