在C中,我试图从文件中获取一个学生信息,并尝试将信息纳入struct,但它不起作用

发布于 2025-01-29 15:23:24 字数 1210 浏览 1 评论 0原文

我的结构格式就是这样

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

,这是我的功能,它试图获取一个学生信息

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);
}

我在其他函数中打开文件,并调用此功能,因为我的目标是尝试将学生信息存储在一个阵列中,该数组是student_ınfo。 TXT文件包含许多学生信息

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

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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北渚 2025-02-05 15:23:24

这是一个示例,包括@retired ninja和@gerhardh所说的错误修复。
文件内容是:

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

现在,该文件用于将每个字段存储为字符串。我对您的代码进行的修复程序如下:

  • student_info声明及其定义是固定的。
  • 添加min_string以遵守最小字符串长度,即1个字符 + 1个空终止。
  • 更改状态字段,以便它们遵守最小字符串空间要求。
  • 添加一些无效检查,也许是为了避免损坏的学生信息。
  • 仅使用一个指针temp来保存由strtok函数返回的指针地址。

此示例代码仅供参考。因此,您将此代码背后的想法调整为实际代码。

#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;
}

这是示例代码的输出:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文