我正在尝试在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: %dn", 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 %dn", 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 Studentnn");
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 %dn", 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: %dn", student.id);
printf("tID: tt %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("ttDisplaying Lessonsn");
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: %dn", id);
int i;
for(i = 0; i < database->numberOfStudents; i++) {
printf("Comparing %d and %dn", id, database->student[i].id);
if(id == database->student[i].id) {
printf("ID found, returning studentn");
return database->student[i];
}
}
Student student;
student.name = malloc(4 * sizeof(char));
student.name = "NULL";
printf("ID not found, returning NULLn");
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 loopn");
for(i = 0; i < db->numberOfStudents; i++) {
printf("=====================nntStudent #%dn", i);
db->student[i] = getNextStudent(studentFile);
printf("ntCompletednn=====================n");
}
}
void swapStudents(Student *student1, Student *student2) {
Student temp;
temp = *student1;
*student1 = *student2;
*student2 = temp;
}
这可能是指针某个地方的错误,通常对指针和C一般。
。所以,当我添加一个新学生时,它会返回以下内容:https://hastebin.com/uwequefiket.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);