'In c I try to get one student information from a file and try to put that informations into struct but it is not working

my struct format is like this

struct {
    char student_ID[11];
    char full_name [MAX];
    char program [MAX];
    char year;
    char e_mail [MAX*2];
    char status;
} student_info;   

And this is my function which tries to get one student information

void scanStudents(FILE *file, student_info *student) {
    char get_line [500];
    fgets(get_line,500,file);

    char *ID = strtok(get_line,";");
    strcpy(student->student_ID, ID);
    char *NAME = strtok(get_line, ";");
    strcpy(student->full_name, NAME);
    char *PROGRAM = strtok(get_line,";");
    strcpy(student->program, PROGRAM);
    char *YEAR = strtok(get_line, ";");
    strcpy(student->year,YEAR);
    char *E_MAIL = strtok(get_line, ";")
    strcpy(student->e_mail,E_MAIL);
    char *STATUS = strtok(get_line,";");
    strcpy(student->status, STATUS);
}

I open file in other function and by calling this function in that my aim is try to store student informations in one array which type is student_ınfo. The txt file contains many student information in type of

31300000010;DURU  AY;Computer Engineering;2;[email protected];


Solution 1:[1]

Here is an example including error fixes that @Retired Ninja and @Gerhardh stated. The file content is:

31300000010;DURU AY;Computer Engineering;2;[email protected];A

Now this one is for storing each field as string. The fixes I made to your code are the following:

  • student_info declaration and its definition are fixed.
  • Add MIN_STRING to comply min String length that is 1 character + 1 NULL termination.
  • Change the year and status fields so that they comply the minimum string space requirements.
  • Add some NULL checks, perhaps to avoid corrupted student info.
  • Used only one pointer temp to hold the pointer address which is returned by strtok function.

This sample code is for reference only. So you adapt the idea behind this code to your actual code.

#include <stdio.h>
#include <string.h>

// Minimum string size must be 2 in order to store 1 character + terminating (NULL or '\0') character
#define MIN_STRING 2
#define MAX (50+1) // 1 character space for the NULL terminator.

struct student_info {
    char student_ID[12];
    char full_name [MAX];
    char program [MAX];
    char year[MIN_STRING];
    char e_mail [MAX];
    char status[MIN_STRING];
};

const char *file_name = "students.txt";

int main(void) {
    
    FILE *file_students = fopen(file_name, "r");
    if(file_students == NULL) {
        printf("The file named %s could not be read\n", file_name);
        return 1; // Return with some failure code
    }
    
    char get_line[500];
    char *temp = NULL;
    struct student_info student;
    
    fgets(get_line, 500, file_students);
    
    temp = strtok(get_line,";");
    strcpy(student.student_ID, temp);
    // After the first invocation of strtok you must pust NULL
    temp = strtok(NULL, ";");
    // strtok returns NULL if there are not any tokens left
    if(temp == NULL) {
        puts("Student ID NULL");
        return 1;
    }
    strcpy(student.full_name, temp);
    temp = strtok(NULL,";");
    if(temp == NULL) {
        puts("Name NULL");
        return 1;
    }
    strcpy(student.program, temp);
    temp = strtok(NULL, ";");
    if(temp == NULL) {
        puts("Program NULL");
        return 1;
    }
    strcpy(student.year,temp);
    temp = strtok(NULL, ";");
    if(temp == NULL) {
        puts("Year NULL");
        return 1;
    }
    strcpy(student.e_mail,temp);
    temp = strtok(NULL,";");
    if(temp == NULL) {
        puts("E-mail NULL");
        return 1;
    }
    strcpy(student.status, temp);
    
    puts("Sample student information");
    printf(
        "ID: %s\nFull name: %s\nProgram: %s\nYear: %s\nE-mail: %s\nStatus: %s\n",
        student.student_ID, student.full_name, student.program, student.year,
        student.e_mail, student.status
           );
    
    // Close the file
    fclose(file_students);
    
    return 0;
}

This is the output of the sample code:

Sample student information
ID: 31300000010
Full name: DURU AY
Program: Computer Engineering
Year: 2
E-mail: [email protected]
Status: A

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kozmotronik