Question: At this point you understand how structures work and what they are used for. This time, we are going to concentrate more on the aspects
At this point you understand how structures work and what they are used for. This time, we are
going to concentrate more on the aspects of using strings. The primary complication this time is
that you are going to have to scan in strings that are often too large to be stored in the structures.
You must not attempt to copy more than the maximum characters that can fit into into one of
these fields. In order to do this, you must truncate strings that are any longer than this. It is also
critical that the strings are properly terminated with a NULL character at the end. You'll also have
to limit the size of the strings when searching.
To handle these nuances, you'll use the strncpy function to copy only as many bytes as are
possible to store and strncmp to limit how many characters of a string participate in a comparison
operation.
Note: strncpy does not NULL terminate long strings. You have to do that your
self!
You are playing a game of Among Us and you want to be able to find imposters. For this homework,
you will implement a way to detect imposters based on the given constraints. You are going to use
a structure that contains a player's information:
username: The gaming handle of the player.
secret: A secret key for the player of a fixed length KEYLEN.
friends: A list of players given by their gaming handles that the player considers trustworthy
to some level. Note that this is unidirectional.
trust: Level of trust for each friend player
The structure is defined as
typedef struct player
char username MAXNAMELEN ;
char secretKEYLEN ;
char friendsMAXFRIENDSMAXNAMELEN ;
int trustMAXFRIENDS ;
playert;
Examine hwh carefully. It contains declarations for the global variables, constants, and the
prototypes for the functions you will write.
The Assignment
You will write a set of functions that deal with arguments of type playert You will modify
occurrences of playert that exist in a global array of length MAXPLAYERS called gplayerarray
that contains playert structures. You will keep track of how many structures you have initialized
using a global variable named gplayercount.
Functions you will write
int readplayerschar infile;
This function should read a list of players from infile. The players should be stored in the
gplayerarray with the first one stored at offset and so on
Sometimes, the secret may be larger than the space available to store it in the playert
structure. In such cases, you should truncate the string appropriately and continue to read
from the file. To facilitate this, you may use a large, temporary buffer to read the string into.
This buffer should be of size MAXBUFFLEN.
Empty friends should be stored as an empty string with a trust level of For example, if there
are only friends, the third entry in friends should be an empty string and the third entry in
trust should be
This function should assert that the argument is not NULL. The function should return the num
ber of players read from the input file. In case an error occurs, it should return an appropriate
error code from the list of error codes in section instead.
Don't forget to update gplayercount.
Input Files
The input file will contain records of the following format:
username$secret$friendfriendfriendM$trusttrusttrustN
A player can have MAXFRIENDS friends, and each friend is preceeded by a Similarly for trust
levels for each friend. Trust levels will be in the same order as the list of friends. Note that the
number of friends and the number of trust levels may be different.
username and each friend is a string, while each trust is an integer.
Error Codes
NONREADABLEFILE: The file cannot be opened for read access.
BADRECORD: The file is incomplete or does not make sense, data is too big to be stored in the
temporary buffer, usernames are not unique, number of friends and number of trust levels
are different.
TOOMUCHDATA: The file contained more than MAXPLAYERS.
NOTFOUND: A player cannot be found in gplayerarray.
NOPLAYERS: The file does not have any players, or there are no players in gplayerarray.
These Standard Rules Apply
You may add any #includes you need to the top of your hwc file.
You may not create any global variables other than those that are provided for you. Creation
of additional global variables will impact your style grade.
You should check for any failures and return an appropriate value.
You should not assume any maximum size for the input files. The test program will generate
files of arbitrary size.
Do not put multiple conditions inside of assert statements.
Please write the function keeping into account the fact that there will be a random amount of friends with trust including you also have to catch if theres more than maxfriends in the file input line and throw an error if that's the case. please don't just use ai i've obviously tried that, i need help
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
