在c中,我尝试从文件中获取一个学生信息,并尝试将这些信息放入结构中,但它不起作用



我的struct格式是这样的

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的数组中。文本文件包含许多学生信息,类型为

31300000010;DURU  AY;Computer Engineering;2;duru.ay@tedu.edu.tr;

这里有一个例子,包括@Retired Ninja和@Gerhardh所述的错误修复。文件内容为:

31300000010;DURU AY;Computer Engineering;2;duru.ay@tedu.edu.tr;A

现在这个用于将每个字段存储为字符串。我对你的代码做了如下修复:

  • student_info声明及其定义是固定的。
  • 添加MIN_STRING以符合最小字符串长度,即1个字符+ 1个NULL终止。
  • 更改yearstatus字段,使其符合最小字符串空间要求。
  • 添加一些NULL检查,也许是为了避免损坏的学生信息。
  • 只使用一个指针temp来保存strtok函数返回的指针地址

此示例代码仅供参考。因此,您可以将这段代码背后的思想应用到您的实际代码中。

#include <stdio.h>
#include <string.h>
// Minimum string size must be 2 in order to store 1 character + terminating (NULL or '') 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 readn", 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: %snFull name: %snProgram: %snYear: %snE-mail: %snStatus: %sn",
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: duru.ay@tedu.edu.tr
Status: A

最新更新