简体   繁体   English

从文件中读取 c 中的结构数组中的 dma 结构时,如何分配 memory

[英]How do you allocate memory when reading from a file for a dma struct in a struct array in c

So I have a map which consists of an array of structs which look like this所以我有一个 map 它由一个结构数组组成,看起来像这样


//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.一旦我开始阅读文件部分,似乎我需要为将存储在图块中的项目声明 memory。 As this would not be a set number as I mentioned in my code how would I go about this?因为这不是我在代码中提到的设定数字,所以我将如何 go 关于这个?

Any alternative aproaches to this process are welcome.欢迎使用此过程的任何替代方法。 Thanks!谢谢!

The map data needs to be serialized when written to the file, and deserialized when read back from the file. map数据写入文件时需要序列化,从文件读回时需要反序列化。 One way to do that is:一种方法是:

for (int y = 0; y < mapSize; y++) {
    for (int x = 0; x < mapSize; y++) {
        fwrite(&map[y][x], offsetof(struct tile, item), 1, mapF);
        fwrite(map[y][x].item, sizeof(struct mapItem), map[y][x].itemAmount, mapF);
    }
}

Error checking has been omitted for clarity.为清楚起见,省略了错误检查。

Reading is similar to writing, but will require memory to be allocated:读取类似于写入,但需要分配 memory:

for (int y = 0; y < mapSize; y++) {
    for (int x = 0; x < mapSize; y++) {
        fread(&map[y][x], offsetof(struct tile, item), 1, mapF);
        map[y][x].item = calloc(map[y][x].itemAmount, sizeof(struct mapItem));
        fread(map[y][x].item, sizeof(struct mapItem), map[y][x].itemAmount, mapF);
    }
}

Again, error checking has been omitted for clarity.同样,为了清楚起见,省略了错误检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM