[Solved] Parsing input data from a text file [closed]


#include <stdio.h>
#include <stdlib.h>

struct classes {
    char name[20];
    char department[5];//+1 for '\0'
    int course_number;
};

int main(void){
    FILE *file;
    char buffer[50];
    struct classes student, students[48];
    int i, count=0;

    file = fopen("inputfile.txt", "r");

    while (fgets(buffer, sizeof(buffer), file)){
        //Format : NAME is enrolled in DEPARTMENT NUMBER.
        if(3 == sscanf(buffer, "%19s %*s %*s %*s %4s %d",
                student.name, student.department, &student.course_number))
                students[count++] = student;
    }
    fclose(file);
    for(i = 0; i < count; ++i){
        printf("%-*s %-*s %d\n",
            (int)sizeof(student.name), students[i].name,
            (int)sizeof(student.department), students[i].department,
            students[i].course_number);
    }
    return 0;
}

3

solved Parsing input data from a text file [closed]