I have to make a program which will print the name and the score of a student found on a text file. The text file itself will follow this format: number of students (int) score name (of student number one) score name (of student number two)
So for example, the students.txt file you will see I am using for this program should be something like this: 2 45 George 23 John
The reading part of the program (for the structures) should be done using fread. I have tried what you will see below and the problem is that when I run the program the console just remains blank. Note that the program is not complete yet, since I am trying to learn how a few things work first (for example I am only trying to print the scores of the student just so I can see whether I know what I am doing) before doing it properly and I know some parts like checking for NULL return are missing. I am also wondering whether since I am using fread the txt file should be considered binary or not.
Here is what I have tried:
#include <stdio.h>
#include <stdlib.h>
#define FILENAME students.txt
#define MAX 50
typedef struct st {
float score;
char name[MAX];
} student;
void read(FILE *fp, student *p, int size);
int main()
{
int number;
student *ptr;
FILE *ifp;
ifp = fopen("FILENAME", "rb");
fscanf(ifp, "%d", &number);
ptr = (student *)malloc(number * sizeof(student));
read(ifp, ptr, number);
}
void read(FILE *fp, student *p, int size)
{
int num;
fscanf(fp,"%d", &num);
fread(p, sizeof(student), size, fp);
for(int i=0; i<size; i++)
printf("%f", p[i].score);
}