【发布时间】:2020-11-22 15:49:39
【问题描述】:
我有一个有 10 行的文件,每行有一个名称、一个 ID 和一个年龄,用空格分隔。问题是当使用 fgets 和 sscanf 读取这个文件时,它会在尝试读取超过 2 行时返回一个 seg 错误。
typedef struct Person
{
char t;
long i;
float a;
} Person;
*arrPersons = (Person*)malloc(10 * sizeof(Person));
int assignInputPeople(Person **arrPersons, FILE *inputPeople)
{
int counter = 0;
char fileLine[344];
long ID;
float ageF;
while (fgets(fileLine, 344, inputPeople) != NULL)
{
sscanf(fileLine, "%s %s %s", arrPersons[counter]->name,&ID)
}
我为我的数组创建了一个指向指针的指针,以便我可以在其他函数中修改它,例如上面的这个。
【问题讨论】:
-
改了还是不行
-
在struct的定义下
-
请发一个Minimal, Reproducible Example,包括
assignInputPeople的调用者和assignInputPeople的左边部分。 -
请edit您的问题包含正确的minimal reproducible example。
-
以及
arrPersons的声明。