因此,任务是读取文件并将数据推送到结构。数据文件:C结构,内存分配和结构
babe 12 red 12
deas 12 blue 12
dsa 12 red 512
bxvx 15 blue 52
reed 18 black 15
而代码是类似的东西
struct shoes {
char name[8];
int size;
char color[8];
int price;
};
//open file
shoes *item=(struct shoes*)malloc(sizeof(struct shoes));
for (i=0; !feof(file); i++) {
item=(struct shoes*)realloc(item,sizeof(struct shoes)+i*sizeof(struct shoes));
fscanf(file,"%s %i %s %i\n",(item+i)->name,(item+i)->size,(item+i)->color,(item+i)->price);
}
但该程序崩溃每次。 012gdbg说:当前上下文中没有符号“item”。 哪里出错?
是的,我用过gcc,所以它工作正常。我会更好地遵循未来的规范) – ovnia
@wingsofovnia我有一个重要的错字,我已经修好了......'鞋子'=鞋子',而不是'鞋子'。 –
为什么要读入临时文件,而不是直接到位? –