我有这个结构和AVL树的结构
struct imobil
{
unsigned int id;
char *streetName;
unsigned int streetNumber;
int noApartaments;
int *noPeopleLivingInApartaments;
float totalValue;
};
struct nodeAVL
{
imobil inf;
nodeAVL *right;
nodeAVL *left;
int BF;
};
我想要做的是用txt文件中的一些文本填充这个AVL结构,当我尝试读取整数int *时它会崩溃。
void main()
{
FILE *f = fopen("Imobil.txt", "r");
int n;
nodeAVL *root = (nodeAVL*)malloc(sizeof(nodeAVL));
imobil imob;
char buffer[20];
fscanf(f, "%d", &n);
for (int i = 0; i < n; i++)
{
fscanf(f, "%d", &root->inf.id);
fscanf(f, "%d", &root->inf.streetNumber);
fscanf(f, "%s", &buffer);
root->inf.streetName= (char*)malloc((strlen(buffer)+1)*sizeof(char));
strcpy(root->inf.streetName, buffer);
fscanf(f, "%f", &root->inf.totalValue);
fscanf(f, "%d", &root->inf.noApartaments);
for (int i = 0; i < root->inf.noApartaments;i++)
fscanf(f, "%d", &root->inf.noPeopleLivingInApartaments[i]);
inserare(imob, root);
}
fclose(f);
preorder(root);
deallocation(root);
}
答案 0 :(得分:2)
在阅读元素之前,您需要为noOfPeopleLivingInApartments
分配空间。
root->inf.noPeopleLivingInApartaments = malloc(root->inf.noApartaments * sizeof(int));
for (int i = 0; i < root->inf.noApartaments;i++) {
fscanf(f, "%d", &root->inf.noPeopleLivingInApartaments[i]);
}