So I have a map which consists of an array of structs which look like this
//Header files
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
//Symbolic names
#define mapSize 301
//This struct will be used to store item data in the map
struct mapItem
{
char name[21];
short id;
short type;
short amount;
}; //End mapItem
//This struct will be used for map generation and storage
struct tile
{
char name[21];
short id;
char description[100];
short itemAmount;
struct mapItem *item;
}; //End tile struct
//This struct variable is the map
struct tile map[mapSize][mapSize];
//Function signatures
void itemGen();
int main()
{
char str[4];
FILE *characterF, *inventoryF, *mapF;
itemGen();
//Opens map file
/*Example file path is C:\*/
strcpy(str, "C:\");
mapF = fopen(str, "w");
//writes map to file
fwrite(map, sizeof(struct tile), (mapSize*mapSize), mapF);
//Closes file
fclose(mapF);
/*This would not usually be streight after the file had been written to
//Opens file
mapF = fopen(str, "w");
//Reads from file
fread(map, sizeof(struct tile), mapSize*mapSize, mapF);
return 0;
} //End main
/*This is just an example in the actual program the itemAmount is not always 3*/
void itemGen()
{
short x, y;
x = y = 100;
//Sets value of itemAmount for example
map[y][x].itemAmount = 3;
//Allocates 3 structs in memory
map[y][x].item = (struct mapItem *)calloc(map[y][x].itemAmount, sizeof(struct mapItem));
//This will add 3 items to the tile
strcpy((map[y][x].item+0)->name, "Apple");
strcpy((map[y][x].item+1)->name, "Bag");
strcpy((map[y][x].item+1)->name, "Bag");
} //End itemGen
Once I get to the reading the file part tho it seems as if I would need to declair memory for the items that will be stored in the tiles. As this would not be a set number as I mentioned in my code how would I go about this?
Any alternative aproaches to this process are welcome. Thanks!
struct mapItem *itemmember in the file, store theitem[0],item[1], ...,item[itemAmount-1]elements instead. One term for this is "flattening" the data, also known as "serializing" the data.