我正在尝试用C语言创建一个小程序,它将读取一个文件,并显示、添加和删除该文件中的数据。
所以,基本上,我有一个预定义的文件(当然它应该可以与任何文件一起工作),就是这个:https://hastebin.com/oficewuveh.pas
当我尝试使用getStudentByID()方法时,在我向文件中添加另一个用户之前,它工作正常。我已经把代码精简到尽可能小的程度,但它仍然很大,很抱歉。

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

typedef struct Lesson {
    char *name;
    int semester;
    float grade;
} Lesson;

typedef struct Student {
    char *name;
    char *surname;
    int id;
    int numberOfLessons;
    Lesson *lesson;
} Student;

typedef struct Database {
    int numberOfStudents;
    Student *student;
} StudentDB;

static int maxNameSize = 100;
static int autoclear = 0;
static int enableTimeout = 0;


void main() {

    system("color 02");
    system("@echo off");

    FILE *studentFile;
    StudentDB database;
    //add = { 0, NULL} ?

    studentFile = fopen("students.txt", "r+w");

    int numberOfStudents;

    //Set the number of students
    fscanf(studentFile, "%d", &database.numberOfStudents);

    //Prints the number of students
    printf("Number of students: %d\n", database.numberOfStudents);

    //Set the memory allocation
    database.student = malloc(database.numberOfStudents * sizeof(Student));
    if(!database.student) {
        printf("The memory allocation has failed. Exiting the program!");
        exit(0);
    }

    //Read the students
    readData(studentFile, &database);
    run(studentFile, &database);

}

Lesson addLesson() {
    Lesson lesson;

    lesson.name = malloc(maxNameSize * sizeof(char));
    if(!lesson.name) { printf("Memory Allocation has failed. Exiting the program!"); exit(0); }

    printf("\tName of the lesson: ");
    scanf("%s", &lesson.name);

    printf("\tSemester: ");
    scanf("%d", &lesson.semester);

    printf("\tGrade: ");
    scanf("%f", &lesson.grade);

    return lesson;
}

void addStudent(StudentDB *database) {
    database->numberOfStudents = database->numberOfStudents + 1;
    printf("\nAdded +1 to number of students, now is %d\n", database->numberOfStudents);
    int numOfStudents = database->numberOfStudents;
    database->student = realloc(database->student, (numOfStudents + 1) * sizeof(Student));

    Student student;
    student.name = malloc(maxNameSize * sizeof(char));
    student.surname = malloc(maxNameSize * sizeof(char));

    if(!student.name) { printf("Memory Allocation has failed. Exiting the program!"); exit(0); }
    if(!student.surname) { printf("Memory Allocation has failed. Exiting the program!"); exit(0); }

    printf("\tAdding a new Student\n\n");

    printf("Name of the student: ");
    scanf("%s", student.name);

    printf("Surname of the student: ");
    scanf("%s", student.surname);

    printf("ID of the student: ");
    scanf("%s", &student.id);

    int numOfLessons;
    printf("How many classes does that student take: ");
    scanf("%d", &numOfLessons);

    int i;
    //Lesson lesson[numOfLessons];
    for(i = 0; i < numOfLessons; i++) {
        printf("Adding lesson %d\n", i);
        student.lesson[i] = addLesson();
    }
    printf("%d", numOfStudents);

//  displayStudent(student);

    database->student[numOfStudents - 1] = student;

}

void displayStudent(Student student) {
    printf("Displaying all the info for the student with ID: %d\n", student.id);

    printf("\tID: \t\t %d  \n", student.id);
    printf("\tFull Name:\t %s %s \n", student.name, student.surname);
    printf("\tLessons :\t %d \n", student.numberOfLessons);
    printf("\t  \n");
    printf("\t\tDisplaying Lessons\n");

    int i;
    for(i = 0; i < student.numberOfLessons; i++) {
        displayLesson(student.lesson[i]);
    }

    system("PAUSE");
}

Student getStudentByID(StudentDB *database) {
    printf("Give us the ID of the student: ");
    int id;
    scanf("%d", &id);

    printf("Done saving the id: %d\n", id);

    int i;
    for(i = 0; i < database->numberOfStudents; i++) {
        printf("Comparing %d and %d\n", id, database->student[i].id);
        if(id == database->student[i].id) {
            printf("ID found, returning student\n");
            return database->student[i];
        }
    }
    Student student;
    student.name = malloc(4 * sizeof(char));
    student.name = "NULL";
    printf("ID not found, returning NULL\n");
    return student;
}

//Successfully gets the lessons. Everything is fine here
Lesson getNextLesson(FILE *studentFile) {
    //Code to read the lesson..
    return lesson;
}

//Successfully gets a student and the lessons. Everything is fine here
Student getNextStudent(FILE *studentFile) {
    //Code to read the student..
    return student;
}

void run(FILE *studentFile, StudentDB *database) {
    int answer;
    //Menu and more code...
    addStudent(database);
}

void readData(FILE *studentFile, StudentDB *db) {
    int i;

    printf("Running the loop\n");
    for(i = 0; i < db->numberOfStudents; i++) {
        printf("=====================\n\n\tStudent #%d\n", i);
        db->student[i] = getNextStudent(studentFile);
        printf("\n\tCompleted\n\n=====================\n");
    }
}

void swapStudents(Student *student1, Student *student2) {
    Student temp;
    temp = *student1;
    *student1 = *student2;
    *student2 = temp;
}

这可能是指针在某个地方的错误,对于指针和一般的C来说还是比较新的。
所以,当我添加一个新的学生时,它返回这个:https://hastebin.com/uwequfiket.coffeescript(看文件的末尾)
我一直想弄清楚我做错了什么,但我没能弄清楚,所以我来到这里。如果有人感兴趣,这里是完整的代码:https://hastebin.com/yolagekire.cpp
提前感谢您的帮助!

最佳答案

printf("ID of the student: ");
scanf("%s", &student.id);

id是一个数字,但在这里,您可以将其作为字符串读取。将代码更改为
printf("ID of the student: ");
scanf("%d", &student.id);

08-17 23:30