In: Computer Science
I am stuck on this problem and I am not sure what the solution is. In C
Write item.h and item.c.
In item.h, typedef a struct (of type t_item) which contains the following information: t_item: char name[MAX_ITEM_NAME_STRING]; char description[MAX_ITEM_DESCRIPTION_STRING];
Make sure that MAX_ITEM_NAME_STRING and MAX_ITEM_DESCRIPTION_STRING are defined with suitable sizes in your item.h.
Typical values are, 25 and 80, respectively. Add the following interface definition to item.h: int item_load_items(t_item items[], int max_items, char *filename);
Returns the number of objects loaded from the filename (or -1 if unable to open the file and load the data). Fills the t_item array items with the contents of a file entitled "items.txt" int item_find_item(t_item items[], int max_items, char *item_name);
Returns the array index of the item with the item_name or -1 if not present. Make sure that you have a header guard around your declarations in item.h Header guards: (https://www.learncpp.com/cpp-tutorial/header-guards/) Implement the item_load_items and item_find_item functions in the item.c file. Use the provided main program to test your item.h and item.c files. Test it with the provided file, items.txt.
Here is my main.
**********************************************************
#include <stdio.h>
#include "item.h"
#define MAX_ITEMS 10
int main()
{
int place;
int num_items = 0;
t_item items[MAX_ITEMS];
if( (num_items = item_load_items(items, MAX_ITEMS, "items.txt"))
> 0 )
{
for(int i=0; i<num_items; i++)
{
printf("%d\t\"%s\"\t\"%s\"\n", i, items[i].name,
items[i].description);
}
for(int i=0; i<num_items; i++)
{
if( (place = item_find_item(items, num_items,
items[i].name)) > -1)
{
printf("Item %s is found at location %d\n",
items[i].name, place);
}
else
{
printf("Can't find %s\n", items[i].name);
}
}
}
else
{
printf("ERROR: Unable to load items\n");
}
}
****************************************************************
This is text file
"kinfe","a large knife"
"spike","a large spike made of metal"
"sword","buster sword"
"bow","reflexive bow"
Code
item.h
#ifndef ITEMH
#define ITEMH
#define MAX_ITEM_NAME_STRING 25
#define MAX_ITEM_DESCRIPTION_STRING 80
typedef struct Item
{
char name[MAX_ITEM_NAME_STRING];
char description[MAX_ITEM_DESCRIPTION_STRING];
}t_item;
int item_load_items(t_item items[], int max_items, char *filename);
int item_find_item(t_item items[], int max_items, char *item_name);
#endif
item.c
#include"item.h"
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
int item_load_items(t_item items[], int max_items, char *filename)
{
int count=0;
FILE *fp = fopen(filename, "r");
char *token,*pos;
int i,len;
if(fp != NULL)
{
char line[120];
while(fgets(line, sizeof(line), fp) != NULL)
{
token = strtok(line, ",");
for(i=0;i<2;i++)
{
if(i==0)
{
strcpy(items[count].name,token);
token = strtok(NULL,",");
} else {
strcpy(items[count].description,token);
if ((pos=strchr(items[count].description, '\n')) != NULL)
*pos = '\0';
}
}
count++;
}
fclose(fp);
}
return count;
}
int item_find_item(t_item items[], int max_items, char *item_name)
{
int i;
for(i=0;i<max_items;i++)
{
if(strcmp(items[i].name, item_name)==0)
return i;
}
return -1;
}
main.c
#include "item.h"
#include <stdio.h>
#define MAX_ITEMS 10
int main()
{
int place;
int num_items = 0;
t_item items[MAX_ITEMS];
if( (num_items = item_load_items(items, MAX_ITEMS, "items.txt")) > 0 )
{
for(int i=0; i<num_items; i++)
{
printf("%d\t\"%s\"\t\"%s\"\n", i, items[i].name, items[i].description);
}
for(int i=0; i<num_items; i++)
{
if( (place = item_find_item(items, num_items, items[i].name)) > -1)
{
printf("Item %s is found at location %d\n", items[i].name, place);
}
else
{
printf("Can't find %s\n", items[i].name);
}
}
}
else
{
printf("ERROR: Unable to load items\n");
}
}
output
If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.