Question

Need this in C

The starter code is long, if you know how to do it in other way please do.

Do the best you can please.

12.15 Assignment: Character Database Character Database In this lab we re-visit our epic battle Characters, but this time weList of commands in main and their associated behaviors i-input Character database from the console • Prompt for and read frovoid readDb (CharacterContainer *characters, char *fileName); • attempt to open the given file for reading binary (mode: rbA Sample Run Menu: (i)nput Characters (w)rite Character file (r)ead Character file (s) ort current list of characters (p)rintName of output file: mariokart.bin 1 items written to mariokart.bin (size header) 6 items written to mariokart.bin (CharaHere's the starter code:

// -----------------------------------------------------------------------

// monsterdb.c

// -----------------------------------------------------------------------

#include

#include

#include

// -----------------------------------------------------------------------

// Some defines

#define NAME_MAX 64

#define BUFFER_MAX 256

// -----------------------------------------------------------------------

// Structs

typedef struct

{

char name[NAME_MAX];

int hp;

int attackPower;

int armor;

} Character;

typedef struct

{

int size;

Character *list;

} CharacterContainer;

// -----------------------------------------------------------------------

// Function prototypes

void printCharacters(CharacterContainer *characters);

void sortCharacters(CharacterContainer *characters);

Character inputCharacter();

void inputCharacters(CharacterContainer *characters);

void readDb(CharacterContainer *characters, char *fileName);

void writeDb(CharacterContainer *characters, char *fileName);

// -----------------------------------------------------------------------

// Main Program

int main()

{

CharacterContainer characters = {0, NULL};

char fileName[BUFFER_MAX];

char userIn[BUFFER_MAX];

printf("Menu:\n(i)nput Characters\n(w)rite Character file\n(r)ead Character file\n");

printf("(s)ort current list of Characters\n(p)rint current list of Characters\n(q)uit\n");

scanf("%s", userIn);

while(userIn[0] != 'q')

{

switch(userIn[0])

{

case 'i':

inputCharacters(&characters);

break;

case 'w':

printf("Name of output file: ");

scanf("%s", fileName);

writeDb(&characters, fileName);

break;

case 'r':

printf("Name of input file: ");

scanf("%s", fileName);

readDb(&characters, fileName);

break;

case 's':

sortCharacters(&characters);

break;

case 'p':

printCharacters(&characters);

break;

default:

printf("Error. Invalid menu choice.\n");

break;

}

printf("Menu:\n(i)nput Characters\n(w)rite Character file\n(r)ead Character file\n");

printf("(s)ort current list of Characters\n(p)rint current list of Characters\n(q)uit\n");

scanf("%s", userIn);

}

return 0;

}

// -----------------------------------------------------------------------

// Function implementations

void printCharacters(CharacterContainer *characters)

{

for (int i = 0; i < characters->size; i++)

{

printf("%s [hp:%d, attack power:%d, armor:%d]\n",

   characters->list[i].name,

   characters->list[i].hp,

   characters->list[i].attackPower,

   characters->list[i].armor);

}

}

void sortCharacters(CharacterContainer *characters)

{

//?? temp;

Character* arr = characters->list;

// Insertion Sort Algorithm...

for (int i = 1; i < characters->size; i++)

{

int j = i;

//while (j >= 1 && ???) ///compare adjacent names with strcmp

{

temp = arr[j];

arr[j] = arr[j - 1];

arr[j - 1] = temp;

j--;

}

}

}

Character inputCharacter()

{

Character ch;

printf("Name: ");

///scanf("%s", ???);

printf("HP: ");

scanf("%d", &ch.hp);

printf("Attack power: ");

///scanf("%d", ???);

printf("Armor: ");

///scanf("%d", ???);

return ch;

}

void inputCharacters(CharacterContainer *characters)

{

printf("Enter number of Characters: ");

///input integer into the struct

///if the list is not NULL, free it to prevent memory leaks

printf("Allocating heap for %d Characters...", characters->size);

///allocate the list

  

///loop through each Character in the list, assign the return value from inputCharacter to the ith element

}

void readDb(CharacterContainer *characters, char *fileName)

{

///Open the file in read binary mode

///make sure it successfully opened

///int nItems = fread(&characters->size, sizeof(??), ??, ??);

///printf("%d items read from '%s' (size header)\n", nItems, fileName);

///if the list is not NULL, free it to prevent memory leaks

printf("Allocating heap for %d Characters...", characters->size);

///allocate the list

///nItems = fread(characters->list, sizeof(???), ???, f);

///printf("%d Characters read from '%s' (Character array)\n", nItems, fileName);

fclose(f);

}

void writeDb(CharacterContainer *characters, char *fileName)

{

///Open the file in write binary mode

///make sure it successfully opened

///int nItems = fwrite(??, ??, ???, ??);

///printf("%d items written to '%s' (size header)\n", nItems, fileName);

///nItems = fwrite(?, ?, ?, ?);

///printf("%d items written to '%s' (Character array)\n", nItems, fileName);

fclose(f); //make sure to close here or writes may not finish!

}

// -----------------------------------------------------------------------


please answer as soon as you can
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// -----------------------------------------------------------------------

// monsterdb.c

// -----------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// -----------------------------------------------------------------------

// Some defines

#define NAME_MAX 64

#define BUFFER_MAX 256

// -----------------------------------------------------------------------

// Structs
typedef struct
{
char name[NAME_MAX];
int hp;
int attackPower;
int armor;
} Character;

typedef struct
{
int size;
Character *list;
} CharacterContainer;

// -----------------------------------------------------------------------
// Function prototypes
void printCharacters(CharacterContainer *characters);
void sortCharacters(CharacterContainer *characters);
Character inputCharacter();
void inputCharacters(CharacterContainer *characters);
void readDb(CharacterContainer *characters, char *fileName);
void writeDb(CharacterContainer *characters, char *fileName);
// -----------------------------------------------------------------------

// Main Program
int main()
{
CharacterContainer characters = {0, NULL};
char fileName[BUFFER_MAX];
char userIn[BUFFER_MAX];
printf("Menu:\n(i)nput Characters\n(w)rite Character file\n(r)ead Character file\n");
printf("(s)ort current list of Characters\n(p)rint current list of Characters\n(q)uit\n");
scanf("%s", userIn);

while(userIn[0] != 'q')
{
switch(userIn[0])
{
case 'i':
inputCharacters(&characters);
break;

case 'w':
printf("Name of output file: ");
scanf("%s", fileName);
writeDb(&characters, fileName);
break;

case 'r':
printf("Name of input file: ");
scanf("%s", fileName);
readDb(&characters, fileName);
break;

case 's':
sortCharacters(&characters);
break;

case 'p':
printCharacters(&characters);
break;

default:
printf("Error. Invalid menu choice.\n");
break;
}
printf("Menu:\n(i)nput Characters\n(w)rite Character file\n(r)ead Character file\n");
printf("(s)ort current list of Characters\n(p)rint current list of Characters\n(q)uit\n");
scanf("%s", userIn);
}

return 0;
}

// -----------------------------------------------------------------------

// Function implementations

void printCharacters(CharacterContainer *characters)
{

for (int i = 0; i < characters->size; i++)
{

printf("%s [hp:%d, attack power:%d, armor:%d]\n",
characters->list[i].name,
characters->list[i].hp,
characters->list[i].attackPower,
characters->list[i].armor);
}

}

void sortCharacters(CharacterContainer *characters)
{
Character temp;

Character* arr = characters->list;

// Insertion Sort Algorithm...
for (int i = 1; i < characters->size; i++)
{

int j = i;
while ((j >= 1) && (strcmp(arr[j-1].name,arr[j].name) > 0)) ///compare adjacent names with strcmp
{

temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
j--;

}
}

}

Character inputCharacter()
{

Character ch;
printf("Name: ");
scanf("%s", ch.name);
printf("HP: ");
scanf("%d", &ch.hp);
printf("Attack power: ");
scanf("%d", &ch.attackPower);
printf("Armor: ");
scanf("%d",&ch.armor);
return ch;
}

void inputCharacters(CharacterContainer *characters)
{

printf("Enter number of Characters: ");
scanf("%d",&characters->size);

///if the list is not NULL, free it to prevent memory leaks
if(characters->list != NULL)
free(characters->list);
printf("Allocating heap for %d Characters...\n", characters->size);
///allocate the list
characters->list = (Character*)malloc(sizeof(Character)*characters->size);

///loop through each Character in the list, assign the return value from inputCharacter to the ith element
for(int i=0;i<characters->size;i++)
{
characters->list[i] = inputCharacter();
}
}

void readDb(CharacterContainer *characters, char *fileName)
{
///Open the file in read binary mode
FILE *f = fopen(fileName,"rb");

///make sure it successfully opened
if(f == NULL)
return;

int nItems = fread(&characters->size, sizeof(int), 1, f);

printf("%d items read from '%s' (size header)\n", nItems, fileName);

///if the list is not NULL, free it to prevent memory leaks
if(characters->list != NULL)
free(characters->list);
printf("Allocating heap for %d Characters...", characters->size);

///allocate the list
characters->list = (Character*)malloc(sizeof(Character)*characters->size);
nItems = fread(characters->list, sizeof(Character), characters->size, f);

printf("%d Characters read from '%s' (Character array)\n", nItems, fileName);

fclose(f);
}

void writeDb(CharacterContainer *characters, char *fileName)
{

///Open the file in write binary mode
FILE *f = fopen(fileName,"wb");
///make sure it successfully opened
if(f == NULL)
{
return;
}
/// read the size of characters list
int nItems = fwrite(&characters->size, sizeof(int), 1, f);

printf("%d items written to '%s' (size header)\n", nItems, fileName);
/// read the characters
nItems = fwrite(characters->list, sizeof(Character), characters->size, f);

printf("%d items written to '%s' (Character array)\n", nItems, fileName);

fclose(f); //make sure to close here or writes may not finish!

}

//end of program

Output:

Menu: (i)nput Characters (w)rite Character file (r)ead Character file Sort current list of Characters (print current list of

Name of output file: mariokart.bin 1 items written to mariokart.bin (size header) 6 items written to mariokart.bin (Chara

Add a comment
Know the answer?
Add Answer to:
Need this in C The starter code is long, if you know how to do it...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I JUST NEED HELP WITH DISPLAY PART! please help! thanks in advance // This function saves...

    I JUST NEED HELP WITH DISPLAY PART! please help! thanks in advance // This function saves the array of structures to file. It is already implemented for you. // You should understand how this code works so that you know how to use it for future assignments. void save(char* fileName) { FILE* file; int i; file = fopen(fileName, "wb"); fwrite(&count, sizeof(count), 1, file); for (i = 0; i < count; i++) { fwrite(list[i].name, sizeof(list[i].name), 1, file); fwrite(list[i].class_standing, sizeof(list[i].class_standing), 1, file);...

  • URGENT. Need help editing some C code. I have done most of the code it just...

    URGENT. Need help editing some C code. I have done most of the code it just needs the following added: takes an input file name from the command line; opens that file if possible; declares a C struct with three variables; these will have data types that correspond to the data types read from the file (i.e. string, int, float); declares an array of C structs (i.e. the same struct type declared in point c); reads a record from the...

  • please complete the missing function only to figure out how many numbers fall within the range...

    please complete the missing function only to figure out how many numbers fall within the range of 90 through 99 total of 29 values in C 6 finclude "lab5.h" 8 const char *FILENAME() - / array of the data file names * ("lab5a.dat", "lab5b.dat", NULL); 12 int main(void) 13 int file count = 0; keeps track of which file we are on/ int check overflow - 0; / counter to prevent array overflow int real filesize = 0; /actual count...

  • MUST BE PROCEDURAL CODE, DO NOT USE GLOBAL VARIABLES. Write a program in C++that generates random...

    MUST BE PROCEDURAL CODE, DO NOT USE GLOBAL VARIABLES. Write a program in C++that generates random words from a training set as explained in the lectures on graphs. Do not hard-code the training set! Read it from a file. A suggested format for the input file: 6 a e m r s t 10 ate eat mate meet rate seat stream tame team tear Here are some suggestions for constants, array declarations, and helper functions #include <iostream> #include <fstream> #include...

  • // Write the compiler used: Visual studio // READ BEFORE YOU START: // You are given...

    // Write the compiler used: Visual studio // READ BEFORE YOU START: // You are given a partially completed program that creates a list of patients, like patients' record. // Each record has this information: patient's name, doctor's name, critical level of patient, room number. // The struct 'patientRecord' holds information of one patient. Critical level is enum type. // An array of structs called 'list' is made to hold the list of patients. // To begin, you should trace...

  • IN C ONLY As mentioned earlier there are two changes we are going to make from...

    IN C ONLY As mentioned earlier there are two changes we are going to make from lab 5, The file you read into data structures can be any length. studentInfo array will be stored in another struct called studentList that will contain the Student pointer and current length of the list. Sometimes data can be used in structs that correlate between variables so it's convenient to store the data in the same struct. Instead of tracking a length variable all...

  • The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also...

    The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also please explain why the seg fault is happening. Thank you #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // @Name loadMusicFile // @Brief Load the music database // 'size' is the size of the database. char** loadMusicFile(const char* fileName, int size){ FILE *myFile = fopen(fileName,"r"); // Allocate memory for each character-string pointer char** database = malloc(sizeof(char*)*size); unsigned int song=0; for(song =0; song < size;...

  • C programming The program will require the following structure: struct _data { char *name; long number;...

    C programming The program will require the following structure: struct _data { char *name; long number; }; The program will require command line arguments: int main(int argv, char **argc) { Where argv is the number of arguments and argc is an array holding the arguments (each is a string). Your program must catch any case where no command line arguement was provided and print a warning message (see below). You MUST include/use the following functions, defined as follows: int SCAN(FILE...

  • // C code // If you modify any of the given code, the return types, or...

    // C code // If you modify any of the given code, the return types, or the parameters, you risk getting compile error. // Yyou are not allowed to modify main (). // You can use string library functions. #include <stdio.h> #include <stdlib.h> #include <string.h> #pragma warning(disable: 4996) // for Visual Studio #define MAX_NAME 30 // global linked list 'list' contains the list of patients struct patientList {    struct patient *patient;    struct patientList *next; } *list = NULL;  ...

  • // READ BEFORE YOU START: // You are given a partially completed program that creates a...

    // READ BEFORE YOU START: // You are given a partially completed program that creates a list of students for a school. // Each student has the corresponding information: name, gender, class, standard, and roll_number. // To begin, you should trace through the given code and understand how it works. // Please read the instructions above each required function and follow the directions carefully. // If you modify any of the given code, the return types, or the parameters, you...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT