I try to take the name and the code of students from a file and save it to a structure. The file look like this
6, student1
2, student2
9, student3
and here is my attempt. Can you tell me what is wrong in this code?
#include <stdio.h>
#include <stdlib.h>
#define N 20
struct Students {
    char studentName[N];
    int code;
};
void student() {
    FILE *f = fopen("file.txt", "r");
    struct Students *ptr;
    int i;
    ptr = malloc(sizeof(struct Students));
    if (ptr == NULL){
        printf("memory cannot be allocated");
        exit(1);
    }
    if (f == NULL) {
        printf("can't open the file");
        exit(1);
    }
    i=0;
    while (feof(f)) {
        fscanf(f, "%d, %s\n", &(ptr+i)->code, &(ptr+i)->studentName);    
        ptr = realloc(ptr, sizeof(struct Students) * i);
        i++;
    }
}
int main() {
    student();
    return 0;
}
 
     
    