繁体   English   中英

C - 结构错误内的指针 (?)

[英]C - Pointer inside Struct Error (?)

我正在尝试用 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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM