Я читаю в записях, использую fscanf и использую условие while, чтобы проверить, что оно считывает ожидаемое количество входов. Тем не менее, он перестает читать в новых записях после того, как прочитал первую. Я не вижу, чтобы положить на это свой палец.
#include <stdio.h>
#include <stdlib.h>
//STRUCTURE
typedef struct{
char name[20];
int age;
float highBP;
float lowBP;
float riskFactor;
} patient;
patient *pRecords[29];
int counter = 0;
int main(int argc, char **argv)
{
int i=0;
for(;i<30;i++){
pRecords[i] = (patient *)malloc(sizeof(patient));
}
FILE *fp;
fp = fopen("data.dat", "r");
if(fp == NULL){
printf("cannot open file\n\n");
return 1;
}
while(fscanf(fp, "name:\t%s\nage:\t%d\nbp:\t%f %f\nrisk:\t%f\n\n", pRecords[counter]->name, &pRecords[counter]->age, &pRecords[counter]->highBP, &pRecords[counter]->lowBP, &pRecords[counter]->riskFactor) == 5){
//printf("%d\n",fscanf(fp, "name:\t%s\nage:\t%d\nbp:\t%f %f\nrisk:\t%f\n\n", pRecords[counter]->name, &pRecords[counter]->age, &pRecords[counter]->highBP, &pRecords[counter]->lowBP, &pRecords[counter]->riskFactor));
printf("%s\n", pRecords[counter]->name);
printf("%d\n", pRecords[counter]->age);
printf("%f\n", pRecords[counter]->highBP);
printf("%f\n", pRecords[counter]->lowBP);
printf("%f\n", pRecords[counter]->riskFactor);
counter++;
}
}
data.dat
name: hank
age: 32
bp: 32.00 32.00
risk: 0.0
name: tom
age: 21
bp: 121.00 81.00
risk: 2.0
name: cindy
age: 32
bp: 190.00 900.00
risk: 5.0